繁体   English   中英

将nowTime作为NSDate对象与其他日期进行比较

[英]get nowTime as NSDate Object to compare to other dates

我需要以24小时格式获取当前时间,以与我从字符串创建的其他nsdate对象进行比较。 在这种情况下,我没有字符串时间,所以我尝试创建一个字符串时间,但是我无法使其正常工作。 这是我的代码:

 let now = NSDate()
   let dateFormatter = NSDateFormatter()
   dateFormatter.timeZone = NSTimeZone.localTimeZone()
   dateFormatter.dateFormat = "MM-dd-yyyy HH:mm"
    let calendar = NSCalendar.currentCalendar()
    let dateString = NSDateFormatter.localizedStringFromDate(now, dateStyle: .ShortStyle, timeStyle: .ShortStyle)
    let currentTime = dateFormatter.dateFromString(dateString)
    print(currentTime)

当前时间为零,不胜感激。

用以下命令替换问题中的所有代码:

let currentTime = NSDate()

而已。 使用该currentTime与其他NSDate对象进行比较。

不需要所有的日期格式和转换。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM