簡體   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