reprinted from: http://blog.csdn.net/jinnchang/article/details/44592801
1. Date (nsdate)
[OBJC]View Plaincopy
- 1. Initialization
- Initializes an object of the current moment
- var now = NSDate ()
- Initializes an object for the current moment of tomorrow
- var tomorrow = NSDate (timeintervalsincenow: 260*60)
- Initializes an object of the current moment yesterday
- var yestoday = nsdate (timeinterval:260*60, sincedate:now) /c2>
- Initializes a time object for 2001-01-01 08:00:00 1 hours after
- var date1 = nsdate (timeintervalsincereferencedate: 3600)
- Initializes a time object for 1970-01-01 08:00:00 1 hours after
- var date2 = NSDate (timeintervalsince1970: 3600)
- 2. Get the time description
- var datedescription = Now. Description
- 3. Get time interval
- Get today to tomorrow's time interval
- var interval1 = tomorrow. Timeintervalsincedate (now)
- Get today to tomorrow's time interval
- var interval2 = tomorrow. Timeintervalsincenow
- Get 2001-01-01 08:00:00 to today's time interval
- var interval3 = Now. Timeintervalsincereferencedate
- Get 1970-01-01 08:00:00 to today's time interval
- var interval5 = Now. timeIntervalSince1970
- 4, randomly return an impossible to achieve the future time, the past time
- Date1 = NSDate. Distantfuture () as NSDate
- Date2 = NSDate. Distantpast () as NSDate
- 5. Time added
- Returns an object for the current moment of the day (based on tomorrow plus one day)
- var thedayaftertomorrow = Tomorrow. Datebyaddingtimeinterval (260*60)
- 6. Time Comparison
- Compares two time objects for the same return Boolean value (Isthesamedate is false due to accuracy issues)
- var isthesamedate = Thedayaftertomorrow. Isequaltodate (NSDate (timeinterval: 260*6 0, sincedate:now))
- Returns an earlier time in two times
- var earlierone = Nowearlierdate (tomorrow)
- Returns a later time in two time
- var laterone = Nowlaterdate (tomorrow)
- Compares two time objects for the same and returns a Nscomparisonresult value
- var compareresult = Nowcompare (Tomorrow)
2. Date conversion (NSDateFormatter)
[OBJC]View Plaincopy
- Method 1: Convert with an existing date format
- var dateformatter1 = nsdateformatter ()
- Dateformatter1. Datestyle = Nsdateformatterstyle. Mediumstyle
- Dateformatter1. Timestyle = Nsdateformatterstyle. Mediumstyle
- var now = NSDate ()
- Date Turn String
- var nowstring = dateformatter1. Stringfromdate (now) //Mar, 9:00:00 PM
- String to Date
- now = dateformatter1. datefromstring (nowstring)!
- Method 2: Custom date format for conversion
- var dateformatter2 = NSDateFormatter ()
- Dateformatter2. DateFormat = "Yyyy-mm-dd HH:mm:ss"
- Date Turn String
- nowstring = Dateformatter2. Stringfromdate (now) //2015-03-24 21:00:00
- String to Date
- now = Dateformatter2. datefromstring (nowstring)!
3. The extension NSDateFormatter provides a way to modify the month, week and other characters, if you are interested, you can refer to the official documents.
A detailed introduction to the Calendar (Nscalendar), date component (nsdatecomponents) is added later.
4. Conclusion
NSDate Class Reference
NSDateFormatter Class Reference
"iOS Learning note" Swift iOS date operation: NSDate, NSDateFormatter (reprint)