【发布时间】:2015-02-09 06:43:44
【问题描述】:
如何在 Swift 中获取本地日期和时间?
let last_login = String(NSDate.date())
【问题讨论】:
-
找到了一个很棒的网站来帮助找出你的格式字符串:nsdateformatter.com
如何在 Swift 中获取本地日期和时间?
let last_login = String(NSDate.date())
【问题讨论】:
更新:Xcode 8.2.1 • Swift 3.0.2
您也可以使用 Date 方法description(with locale: Locale?) 来获取用户的本地化时间描述:
日期的字符串表示,使用给定的语言环境,或者如果语言环境 参数为零,采用国际格式 YYYY-MM-DD HH:MM:SS ±HHMM,其中 ±HHMM 表示时区偏移(以小时为单位), 距 UTC 分钟数(例如,“2001-03-24 10:45:32 +0600”)。
description(with locale: Locale?)
Date().description(with: .current) // "Monday, February 9, 2015 at 05:47:51 Brasilia Summer Time"
上面的方法不打算在向用户显示日期和时间时使用。它仅用于调试目的。
向用户显示本地日期和时间(当前时区)时,您应该尊重用户的区域设置和设备设置。您唯一可以控制的是日期和时间样式(短、中、长或完整)。有关更多信息,您可以查看此帖子shortDateTime。
如果您的意图是为编码目的创建时间戳 UTC (iso8601),您可以查看此帖子 iso8601
【讨论】:
如果你想得到一个 Date 对象 而不是字符串表示,你可以使用下面的 sn-p:
extension Date {
func localDate() -> Date {
let nowUTC = Date()
let timeZoneOffset = Double(TimeZone.current.secondsFromGMT(for: nowUTC))
guard let localDate = Calendar.current.date(byAdding: .second, value: Int(timeZoneOffset), to: nowUTC) else {return Date()}
return localDate
}
}
像这样使用它:
let now = Date().localDate()
【讨论】:
let localDate = Date().localDate() 然后print(localDate.description(with: .current)) 看看结果
Leo 的回答很棒。我只是想添加一种将其用作计算属性的方法。
var currentTime: String {
Date().description(with: .current)
}
像这样使用它:
print(currentTime)
或者你可以封装它:
extension String {
static var currentTime: String {
Date().description(with: .current)
}
}
然后你可以在任何你使用字符串的地方使用它:
var time: String = .currentTime
【讨论】:
通过设置格式来使用 NSDateFormatter
let dateFormatter = NSDateFormatter()
dateFormatter.dateFormat = "hh:mm"
println(dateFormatter.stringFromDate(NSDate()))
或样式
dateFormatter.dateStyle = .NoStyle
dateFormatter.timeStyle = .MediumStyle
【讨论】:
我已经找到答案了。
let dateFormatter = NSDateFormatter()
dateFormatter.dateFormat = "MM-dd-yyyy HH:mm"
let dateInFormat = dateFormatter.stringFromDate(NSDate())
【讨论】:
let expiryDate: Date = ...
let localizedDateString = DateFormatter.localizedString(from: expiryDate, dateStyle: .medium, timeStyle: .short)
“2017 年 9 月 10 日,14:37”
【讨论】:
获取最常见的字符串格式(在处理查询和数据库时):
斯威夫特 4、5
2019-01-09T01:07:04Z(RFC3339 GMT/祖鲁时间)
let f = ISO8601DateFormatter()
f.formatOptions = [.withInternetDateTime]
let s = f.string(from: Date())
2019-01-08T17:04:16-08:00(RFC3339 占本地时区)
let f = ISO8601DateFormatter()
f.formatOptions = [.withInternetDateTime]
f.timeZone = TimeZone.current
let s = f.string(from: Date())
2019-01-09(格林威治标准时间/祖鲁时间的标准日期戳)
let f = ISO8601DateFormatter()
f.formatOptions = [.withFullDate, .withDashSeparatorInDate]
let s = f.string(from: Date())
2019-01-08(本地时区的标准日期戳记)
let f = ISO8601DateFormatter()
f.formatOptions = [.withFullDate, .withDashSeparatorInDate]
f.timeZone = TimeZone.current
let s = f.string(from: Date())
所有四个字符串都代表完全相同的时间点。请记住,按字母顺序对这些字符串进行排序也会按时间顺序对它们进行排序,这使得这个数据数据库不可知(我一直致力于)。
【讨论】:
你必须使用 NSDateFormatter
let dateFormatter = NSDateFormatter()
dateFormatter.dateFormat = "MMMM dd, yyyy HH:mm"
dateFormatter.locale = "en" // Change "en" to change the default locale if you want
let stringDate = dateFormatter.stringFromDate(date)
【讨论】:
获取当前日期和时间
let currentDate = Date()
print(currentDate) //this will return current date and time
但那将是日期类型以将日期转换为字符串
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "dd-MM-yyyy HH:mm" //give the formate according to your need
let dateStr = dateFormatter.string(from: currentDate) //which will give the string of current date and time in required dateformate
【讨论】: