to parse from a string to get an accurate time, you must specify 正确的格式字符串 , as well as the corresponding时区标示
1. Date format
1. Year
y Displays the year (0-9) as a number without leading zeros
- yy Displays the year in a two-digit number format with leading zeros
- yyy display year in four-digit format
- yyyy display year in four-digit format
2. Month
m Displays the month as a number without a leading zero (e.g. January as 1)
- mm< /code> Displays the month as a number with leading zeros (for example, 01/12/01)
- mmm Displays the month as an abbreviated form (for example, Jan)
Li style= "> mmmm display month as full month name (e.g. January)
- January Jan January
- April Apr April
- May May
- July Jul July
- September SEP September
- October Oct October
- December Dec December
3. Day
dDisplays the day as a number without leading zeros (such as 1)
ddDisplays the day as a number with leading zeros (for example, 01)
4. Week
eee & nbsp; display the day as an abbreviated form (for example, Sun)
- eeee Displays the day as full name (for example Sunday)
- Monday Mon Monday
- Tuesday Tue Tuesday
- Wednesday Wed Wednesday
- Thursday Thu Thursday
- Friday Fri Friday
- Saturday Sat Saturday
- Sunday Sun Sunday
5. Hours p>
h Use a 12-hour system to display hours as numbers without leading zeros (for example, 1:15:15 PM)
- hh using 12 Hourly displays the hour as a number with leading zeros (for example, 01:15:15 PM)
- h Displays the hour as a number without leading zeros using the 24-hour system (for example, 1:15:15). /li>
hh use 24-hour system to display hours as a number with leading zeros (for example, 01:15:15)
6. Minutes
m Displays the minute as a number without leading zeros (for example, 12:1:15)
- mm Displays the minute as a number with leading zeros (for example, 12:01:15)
/ul>
7. Sec
s display seconds as a number without leading zeros (for example, 12:15:5)
ss Displays the second as a number with leading zeros (for example, 12:15:05)
f Displays the decimal portion of the seconds
ff will be displayed exactly to 1% seconds
- ffff Accurate display to one out of 10,000 seconds
- up to seven F symbols can be used in user-defined formats
8. Morning, afternoon
t Use 12-hour
- any hour before noon displays uppercase a
- any hour between noon to 11:59 PM displays uppercase P
tt for locales using 12-hour
- show uppercase AM
- Noon to 11:59 Any hour between PM displays uppercase PM
9. Time zone
z Display time zone offset without leading zeros
- zz display time zone offset with leading zeros (for example -08)
zzz Displays the full time zone offset (for example -0800)
10. Example
///create date based on specified date string//datesring Date string///Date string format: "Sun May 24 13:59:03 +0800 "Class Func CreateDate (datesring:string), nsdate? {Let df = nsdateformatter () Df.locale = Nslocale (localeidentifier: "ch") Df.dateformat = "EEE MMM dd HH:mm:ss zzz yyyy" retur n df.datefromstring (datesring)}
2. Create categories for NSDate
Parse from string to exact time//parameter datestring for time string formatString for time string corresponds to format class func getDate (Datestring:string, formatString : String), NSDate? {//dateformatter//1. Instantiate let df = NSDateFormatter ()//2. To specify the date of the region, Xcode 6.3 Beta is not required, the region needs to specify English//NOTE: When debugging a real machine, be sure to specify the zone, otherwise The same version cannot be converted Df.locale = Nslocale (localeidentifier: "en")//3. Set Date Format Df.dateformat = formatstring//4. Build Date return df.datefromstring (datestring)}
3. Date Processing class Nscalendar
。。。
IOS Date processing-date resolved