gpt4 book ai didi

ios - 设置当前时间为yyyy-MM-dd 00 :00:00 using Swift

转载 作者:行者123 更新时间:2023-11-28 07:50:19 24 4
gpt4 key购买 nike

我想问一下NSDate,如何设置/格式化当前时间,如“2015-08-12 09:30:41 +0000”到“2015-08-12 00:00: 00 +0000

我已经在使用了:

var dateFormatter = NSDateFormatter()
dateFormatter.dateStyle = .MediumStyle
dateFormatter.timeStyle = .NoStyle
//result date will be : Aug 12, 2015

但值日期在数据库中存储的并不完全为“2015-08-12 00:00:00 +0000”,而是存储为“2015-08-11 17:00:00 +0000 UTC”

最佳答案

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"
}

关于ios - 设置当前时间为yyyy-MM-dd 00 :00:00 using Swift,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49856237/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com