IOS Date Processing and ios Date Processing
To obtain accurate time from a string, you must specifyCorrect Format StringAnd the correspondingTime zone ID
1. Date Format
1. Year
yDisplay the year (0-9) as a number without leading zero
yyDisplays the year in the format of two digits with leading zeros.
yyyDisplay the year in four-digit format
yyyyDisplay the year in four-digit format
2. Month
MDisplay the month as a number without leading zero (for example, January 1, January is expressed as 1)
MMDisplay the month as a number with a leading zero (for example, 01/12/01)
MMMDisplay the month as an abbreviation (for example, Jan)
MMMMDisplay the month as the complete month name (for example, January)
- Jan January
- Feb February
- Mar March, March
- Apr April
- May
- June Jun June
- July Jul July
- Aug August, August
- Sep September
- October, October
- Nov November, November
- December, December
3. Day
dDisplay the day as a number without leading zero (for example, 1)
ddDisplay the day as a number with a leading zero (for example, 01)
4. Weeks
EEEDisplay Day as an abbreviation (for example, Sun)
EEEEDisplay day as full name (for example, Sunday)
- Mon Monday
- Tuesday Tue Tuesday
- Wed Wednesday
- Thu Thursday
- Friday Fri Friday
- Saturday
- Sunday Sun
5. Hours
hUse the 12-hour system to display the hour as a number without leading zero (for example, 1:15:15)
hhDisplay the hour as a number with a leading zero (for example, 01:15:15) in 12-hour format)
HUse the 24-hour system to display the hour as a number without leading zero (for example, 1:15:15)
HHDisplay the hour as a number with a leading zero (for example, 01:15:15) in the 24-hour format)
6. Minutes
mDisplay the minute as a number without leading zero (for example)
mmDisplay minutes as numbers with leading zeros (for example, 12:01:15)
7. Seconds
sDisplay the second as a number without leading zero (for example)
ssDisplay seconds as numbers with leading zeros (for example, 12:15:05)
fDisplays the decimal part of a second.
ffDisplay to 1% seconds
ffffDisplay the data in exactly one thousandth of a second
- Up to seven f characters can be used in user-defined formats
8. Morning and afternoon
t12-hour format
- An upper-case A is displayed for any hour before noon.
- An uppercase P value is displayed for any hour between noon and PM.
ttFor regions in the 12-hour format
- The upper-case AM is displayed for any hour before noon.
- An uppercase PM is displayed for any hour between noon and PM.
- For regions in the 24-hour format, no characters are displayed.
9. Time Zone
zDisplay Time Zone offset without leading zero
zzDisplay the time zone offset with leading zero (for example,-08)
zzzDisplay the complete time zone offset (for example,-0800)
10. Example
/// Create date based on the specified date string /// dateSring date string // The format of the date string is: "Sun May 24 13:59:03 + 0800 2015" class func createDate (dateSring: string)-> NSDate? {Let df = NSDateFormatter () df. locale = NSLocale (localeIdentifier: "ch") df. dateFormat = "eee mmm dd HH: mm: ss zzz yyyy" return df. dateFromString (dateSring )}
2. Create a category for NSDate
/// Parse from String to accurate time // The dateString parameter is the time String formatString. The formatString format is class func getDate (dateString: String, formatString: String)-> NSDate? {// DateFormatter // 1. instantiate let df = NSDateFormatter () // 2. to specify the region of the date, Xcode 6.3 beta is not required. You must specify the English language for the region. // Note: When debugging a real machine, you must specify the region, otherwise, df cannot be converted in previous versions. locale = NSLocale (localeIdentifier: "en") // 3. set the date format df. dateFormat = formatString // 4. generation date return df. dateFromString (dateString )}
3. Date Processing class NSCalendar
...