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

ぃ、小莉子 提交于 2019-12-02 07:00:57
Leo Dabus
let dateString = "2015-08-12 09:30:41 +0000"
let dateFormatter = NSDateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd"
dateFormatter.calendar = NSCalendar(calendarIdentifier: NSCalendarIdentifierGregorian)
dateFormatter.timeZone = NSTimeZone(forSecondsFromGMT: 0)
dateFormatter.locale = NSLocale(localeIdentifier: "en_US_POSIX")
if let dateFromString = dateFormatter.dateFromString(dateString.componentsSeparatedByString(" ").first ?? "") {
    println(dateFromString)  // "2015-08-12 00:00:00 +0000"
}

Swift 4

let dateString = "2019-05-04 09:30:41 +0000"
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd"
dateFormatter.calendar = Calendar(identifier: .gregorian)
dateFormatter.timeZone = TimeZone(secondsFromGMT: 0)
dateFormatter.locale = Locale(identifier: "en_US_POSIX")
if let dateFromString = dateFormatter.date(from: dateString.components(separatedBy: " ").first ?? "") {
    print(dateFromString)  // "2019-05-04 00:00:00 +0000"
}
vadian

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)

If you only care about the timezone on user's device, i.e, you are not going to save that formatted date string to server, etc, then, you can use the following code:

var formatter: NSDateFormatter = NSDateFormatter()
formatter.dateFormat="yyyy-MM-dd 00:00:00 Z"
formatter.stringFromDate(date)

If you want to save that date string to server as well, then, you should add timezone info to the formatter:

var formatter: NSDateFormatter = NSDateFormatter()
formatter.timeZone = NSTimeZone(forSecondsFromGMT: 0)
formatter.dateFormat="yyyy-MM-dd 00:00:00 Z"
formatter.stringFromDate(date)

Update

Now I understand what you actually wanted to do, you have a date string instead of a NSDate object as an input.

You can use the following code for achieving your desired output, with respect to preserving timezone info of the input.

let dateString = "2015-08-12 09:30:41 +0000"
let calendar = NSCalendar(calendarIdentifier: NSCalendarIdentifierGregorian)
let timeZone = NSTimeZone(forSecondsFromGMT: 0)

let inputDateFormatter = NSDateFormatter()
inputDateFormatter.dateFormat = "yyyy-MM-dd hh:mm:ss Z"
inputDateFormatter.calendar = calendar

if let inputDate = inputDateFormatter.dateFromString(dateString) {
    let outPutDateFormatter = NSDateFormatter()
    outPutDateFormatter.calendar = calendar
    outPutDateFormatter.timeZone = timeZone
    outPutDateFormatter.dateFormat = "yyyy-MM-dd 00:00:00 Z"
    print(outPutDateFormatter.stringFromDate(inputDate))
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!