在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
SWIFT中如何计算两个日期间隔多少个小时,其实几句代码就可以搞定了,Very Easy,在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 附上Playgroud图片 如果要查看两个日期间隔多少天又该怎么做呢? 上面components的第一个参数为Hour那么应该也有Day吧,转到定义内看到如下的静态只读属性,改下这个参数后就可以获取间隔多少小时了。 static var CalendarUnitEra: NSCalendarUnit { get } static var CalendarUnitYear: NSCalendarUnit { get } static var CalendarUnitMonth: NSCalendarUnit { get } static var CalendarUnitDay: NSCalendarUnit { get } static var CalendarUnitHour: NSCalendarUnit { get } static var CalendarUnitMinute: NSCalendarUnit { get } static var CalendarUnitSecond: NSCalendarUnit { get } static var CalendarUnitWeekday: NSCalendarUnit { get } static var CalendarUnitWeekdayOrdinal: NSCalendarUnit { get }
|
请发表评论