How many hours of two date intervals are calculated in Swift

Source: Internet
Author: User
Tags date1

How to calculate two date intervals in swift How many hours, in fact a few lines of code can be done, Very easy, enter the following code within the playground

var date1 = "2015-06-26 8:15:10" var dateformatter = NSDateFormatter () Dateformatter.dateformat = "Yyyy-mm-dd Hh:mm:ss" var Dateresult = dateformatter.datefromstring (date1) nsdate () var Gregorian = Nscalendar (calendaridentifier: Nscalendaridentifiergregorian) var result = gregorian!. Components (Nscalendarunit.calendarunithour, fromdate:dateresult!, Todate:nsdate (), options:nscalendaroptions (0)) Result.hour

Attached Playgroud Pictures

What do you do if you want to see how many days are two dates apart?

The first parameter of the components above is hour so there should be a day, go to the definition to see the following static read-only property, change this parameter to get the interval of how many hours.

    static Var calendarunitera:nscalendarunit {get}    static var calendarunityear:nscalendarunit {get}    static var Calendarunitmonth:nscalendarunit {get}    static var calendarunitday:nscalendarunit {get}    static var calendarun Ithour:nscalendarunit {get}    static var calendarunitminute:nscalendarunit {get}    static var Calendarunitsecond : nscalendarunit {get}    static var calendarunitweekday:nscalendarunit {get}    static var Calendarunitweekdayordi Nal:nscalendarunit {Get}

How many hours of two date intervals are calculated in Swift

Related Article

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.