Set current time to yyyy-MM-dd 00:00:00 using Swift

后端 未结 3 778
广开言路
广开言路 2021-01-25 02:25

I want to ask about NSDate, how to set/format current time like \"2015-08-12 09:30:41 +0000\" to \"2015-08-12 00:00:00 +0000

I\'m already using :

         


        
3条回答
  •  说谎
    说谎 (楼主)
    2021-01-25 02:50

    The easiest way to remove the time portion from an NSDate instance is

    let startOfDate = NSCalendar.currentCalendar().startOfDayForDate(NSDate())
    

    To get the date depending on the current time zone use

    let calendar = NSCalendar(identifier: NSCalendarIdentifierGregorian)!
    calendar.timeZone = NSTimeZone(forSecondsFromGMT: 0)
    let startOfDate = calendar.startOfDayForDate(NSDate())
    println(startOfDate)
    

    Swift 4

    var calendar = Calendar(identifier: .gregorian)
    calendar.timeZone = TimeZone(secondsFromGMT: 0)!
    let startOfDate = calendar.startOfDay(for: Date())
    print(startOfDate)
    

提交回复
热议问题