IOS Date Processing-resolution date
To obtain accurate time from a string, you must specifyCorrect Format String
And the correspondingTime zone ID
1. Date Format
1. Year
y
Display the year (0-9) as a number without leading zero
yy
Displays the year in the format of two digits with leading zeros.
yyy
Display the year in four-digit format
yyyy
Display the year in four-digit format
2. Month
M
Display the month as a number without leading zero (for example, January 1, January is expressed as 1)
MM
Display the month as a number with a leading zero (for example, 01/12/01)
MMM
Display the month as an abbreviation (for example, Jan)
MMMM
Display 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
d
Display the day as a number without leading zero (for example, 1)
dd
Display the day as a number with a leading zero (for example, 01)
4. Weeks
EEE
Display Day as an abbreviation (for example, Sun)
EEEE
Display day as full name (for example, Sunday)
- Mon Monday
- Tuesday Tue Tuesday
- Wed Wednesday
- Thu Thursday
- Friday Fri Friday
- Saturday
- Sunday Sun
5. Hours
h
Use the 12-hour system to display the hour as a number without leading zero (for example, 1:15:15)
hh
Display the hour as a number with a leading zero (for example, 01:15:15) in 12-hour format)
H
Use the 24-hour system to display the hour as a number without leading zero (for example, 1:15:15)
HH
Display the hour as a number with a leading zero (for example, 01:15:15) in the 24-hour format)
6. Minutes
m
Display the minute as a number without leading zero (for example)
mm
Display minutes as numbers with leading zeros (for example, 12:01:15)
7. Seconds
s
Display the second as a number without leading zero (for example)
ss
Display seconds as numbers with leading zeros (for example, 12:15:05)
f
Displays the decimal part of a second.
ff
Display to 1% seconds
ffff
Display 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
t
12-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.
tt
For 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
z
Display Time Zone offset without leading zero
zz
Display the time zone offset with leading zero (for example,-08)
zzz
Display 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
...