- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
当我尝试记录当前日期时:
print(NSDate())
或
print(Date())
(在 Swift 3 中)
或者任何日期对象,它显示错误的时间。比如现在大概是16点12分,但是上面显示的是
2016-10-08 20:11:40 +0000
我的约会时区是否错误?如何修正我的日期以获得正确的时区?
为什么会这样,如何解决?如何在打印语句或调试器中轻松显示本地时区的任意日期?
(请注意,这个问题是一个“铃声”,以便我可以提供一个简单的 Swift 3/Swift 2 Date/NSDate 扩展,让您轻松显示本地时区的任何日期对象。
最佳答案
NSDate(或 Swift ≥ V3 中的日期)没有时区。它记录了世界各地的时间瞬间。
在内部,日期对象记录自“纪元日期”以来的秒数,或 2001 年 1 月 1 日午夜 Greenwich Mean Time 中的秒数。 ,又名 UTC。
我们通常会考虑本地时区的日期。
如果您使用记录日期
print(NSDate())
系统显示当前日期,但以 UTC/格林威治标准时间表示。因此,时间看起来正确的唯一地方就是该时区。
如果您发出调试器命令,您会在调试器中遇到相同的问题
e NSDate()
这是一个痛苦。我个人希望 iOS/Mac OS 能够使用用户当前时区显示日期,但事实并非如此。
对我之前使用本地化字符串的改进是创建 Date
类的扩展,使其更易于使用:
extension Date {
func localString(dateStyle: DateFormatter.Style = .medium, timeStyle: DateFormatter.Style = .medium) -> String {
return DateFormatter.localizedString(from: self, dateStyle: dateStyle, timeStyle: timeStyle)
}
}
这样你就可以使用像 Date().localString()
这样的表达式,或者如果你只想打印时间,你可以使用 Date().localString(dateStyle :.无)
我刚刚发现NSDateFormatter
(Swift 3中的DateFormatter
)有一个类方法localizedString。这与我下面的扩展的作用相同,但更简单、更干净。以下是声明:
class func localizedString(from date: Date, dateStyle dstyle: DateFormatter.Style, timeStyle tstyle: DateFormatter.Style) -> String
所以你只需使用
let now = Date()
print (DateFormatter.localizedString(
from: now,
dateStyle: .short,
timeStyle: .short))
您几乎可以忽略下面的所有内容。
<小时/>我创建了 NSDate 类的一个类别(Swift 3 中的 Date),它有一个 localDateString 方法,可以显示用户本地时区的日期。
这是 Swift 3 形式的类别:(文件名 Date_displayString.swift)
extension Date {
@nonobjc static var localFormatter: DateFormatter = {
let dateStringFormatter = DateFormatter()
dateStringFormatter.dateStyle = .medium
dateStringFormatter.timeStyle = .medium
return dateStringFormatter
}()
func localDateString() -> String
{
return Date.localFormatter.string(from: self)
}
}
以 Swift 2 形式:
extension NSDate {
@nonobjc static var localFormatter: NSDateFormatter = {
let dateStringFormatter = NSDateFormatter()
dateStringFormatter.dateStyle = .MediumStyle
dateStringFormatter.timeStyle = .MediumStyle
return dateStringFormatter
}()
public func localDateString() -> String
{
return NSDate.localFormatter.stringFromDate(self)
}
}
(如果您喜欢不同的日期格式,可以很容易地修改日期格式化程序使用的格式。也可以直接显示您需要的任何时区的日期和时间。)
我建议将此文件的相应 Swift 2/Swift 3 版本放入您的所有项目中。
然后您可以使用
swift 2:
print(NSDate().localDateString())
swift 3:
print(Date().localDateString())
关于swift - NSDate() 或 Date() 显示错误的时间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57280201/
数据框有一个字符串类型的日期列 '2017-01-01' 它被转换为 DateType() df = df.withColumn('date', col('date_string').cast(Dat
这个问题在这里已经有了答案: What is "x && foo()"? (5 个答案) 关闭 8 年前。 我在 bootstrap-datepicker.js 文件中遇到过这个。 作者在_setD
我有一个数据库 utc 字符串,我正在传递到 Date(attrs.endDate),然后通过 new Date() 减去当前的 utc 日期,但我无法得到它来为我提供 2 个 utc 日期的正确差异
这个问题在这里已经有了答案: how to determine if 2 dates object equals each other? [duplicate] (3 个答案) 关闭 6 年前。 我
这个问题已经有答案了: How can I convert "/Date(1399739515000)/" into date format in JavaScript? (3 个回答) 已关闭 8
根据MDN ,我们只能将以下类型的参数传递给 Date 构造函数: new Date(); new Date(value); // Unix timestamp new Date(dateString
我从表中获取所有项目: endDate >= 现在 endDate 为 NULL published 等于 1。 这是我所拥有的,但它给了我 0 个项目: $items = Items::orderB
此查询需要很长时间才能完成。当我将 WHERE 子句设置为 new_dl >= '2014-01-01' 时,查询大约需要 6 分钟才能浏览大约 3 个月的数据。现在不知道为什么这个应该从 12 个月
我有一个正在为项目开发的小型 Java 程序,它使用 JavaMail 从指定的 URI 中提取用户的收件箱,然后开始处理消息。 在 Outlook 中,属性菜单中有一个功能可以设置邮件的到期日期,它
我想在获取 Date.getHours()、Date.getMinutes() 和 Date.getSeconds() 的值后格式化输出>. 这是一条漫长的路: var dt = new Date()
我发现java.text.DateFormat有两种格式化日期的方法。一种是采用 Date 参数,另一种是采用 Object 参数。我检查了DateFormat源代码,似乎他们调用了不同的内部方法。
我有两个对象,p4 和 p5,它们都具有 Date 属性。在某些时候,构造函数工作正常: p4.setClickDate(new Date(System.currentTimeMillis() - 8
我是使用 Sequelize 和 Node.js 的新手,但我的代码中存在日期比较问题。 User.findOne({ where: { resetToken: passwordToken,
我正在使用一个名为 fullcalendar 的 jquery 日历。当用户单击某一天时,他们将被发送到另一个页面以创建该天的事件。单击的日期作为 date 提供。然后通过下面的函数运行将其转换为 U
我有一个列表列表,每个列表中都有整数值,代表 8 年期间的日期。 dates = [[2014, 11, 14], [2014, 11, 13], ....., [2013, 12, 01]
我有两个表: 首先是TimeValues(示例) time | value 12/28/18 | 5.6 01/03/19 | 5.6 01/04/19 | 5.6 01/09/19 | 5.
关闭。这个问题需要debugging details .它目前不接受答案。 编辑问题以包含 desired behavior, a specific problem or error, and th
像这样实例化的日期对象: new Date("2011-12-13") 返回一个认为自己是星期一的日期对象: Date {Mon Dec 12 2011 16:00:00 GMT-0800 (PST)
我需要选择入住日期和退房日期在指定日期范围之间的房价。这些费率根据其条件单独命名。房费取决于所选日期。这是我的代码: rate_eb rate_name rate_starts rat
我有 [Int64:[String:String]] 其中 Int64 是时间戳。如何检测和删除 [String:String] 中的参数之一是 ["name"] = "test" 并重复多次的同一天
我是一名优秀的程序员,十分优秀!