"iOS Learning note" Swift iOS date operation: NSDate, NSDateFormatter (reprint)

Source: Internet
Author: User
Tags vars

reprinted from: http://blog.csdn.net/jinnchang/article/details/44592801
1. Date (nsdate) [OBJC]View Plaincopy
  1. 1. Initialization
  2. Initializes an object of the current moment
  3. var now = NSDate ()
  4. Initializes an object for the current moment of tomorrow
  5. var tomorrow = NSDate (timeintervalsincenow: 260*60)
  6. Initializes an object of the current moment yesterday
  7. var yestoday = nsdate (timeinterval:260*60, sincedate:now) /c2>
  8. Initializes a time object for 2001-01-01 08:00:00 1 hours after
  9. var date1 = nsdate (timeintervalsincereferencedate: 3600)
  10. Initializes a time object for 1970-01-01 08:00:00 1 hours after
  11. var date2 = NSDate (timeintervalsince1970: 3600)
  12. 2. Get the time description
  13. var datedescription = Now. Description
  14. 3. Get time interval
  15. Get today to tomorrow's time interval
  16. var interval1 = tomorrow. Timeintervalsincedate (now)
  17. Get today to tomorrow's time interval
  18. var interval2 = tomorrow. Timeintervalsincenow
  19. Get 2001-01-01 08:00:00 to today's time interval
  20. var interval3 = Now. Timeintervalsincereferencedate
  21. Get 1970-01-01 08:00:00 to today's time interval
  22. var interval5 = Now. timeIntervalSince1970
  23. 4, randomly return an impossible to achieve the future time, the past time
  24. Date1 = NSDate. Distantfuture () as NSDate
  25. Date2 = NSDate. Distantpast () as NSDate
  26. 5. Time added
  27. Returns an object for the current moment of the day (based on tomorrow plus one day)
  28. var thedayaftertomorrow = Tomorrow. Datebyaddingtimeinterval (260*60)
  29. 6. Time Comparison
  30. Compares two time objects for the same return Boolean value (Isthesamedate is false due to accuracy issues)
  31. var isthesamedate = Thedayaftertomorrow. Isequaltodate (NSDate (timeinterval: 260*6 0, sincedate:now))
  32. Returns an earlier time in two times
  33. var earlierone = Nowearlierdate (tomorrow)
  34. Returns a later time in two time
  35. var laterone = Nowlaterdate (tomorrow)
  36. Compares two time objects for the same and returns a Nscomparisonresult value
  37. var compareresult = Nowcompare (Tomorrow)
2. Date conversion (NSDateFormatter) [OBJC]View Plaincopy
  1. Method 1: Convert with an existing date format
  2. var dateformatter1 = nsdateformatter ()
  3. Dateformatter1. Datestyle = Nsdateformatterstyle. Mediumstyle
  4. Dateformatter1. Timestyle = Nsdateformatterstyle. Mediumstyle
  5. var now = NSDate ()
  6. Date Turn String
  7. var nowstring = dateformatter1. Stringfromdate (now) //Mar, 9:00:00 PM
  8. String to Date
  9. now = dateformatter1. datefromstring (nowstring)!
  10. Method 2: Custom date format for conversion
  11. var dateformatter2 = NSDateFormatter ()
  12. Dateformatter2. DateFormat = "Yyyy-mm-dd HH:mm:ss"
  13. Date Turn String
  14. nowstring = Dateformatter2. Stringfromdate (now) //2015-03-24 21:00:00
  15. String to Date
  16. 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)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.