NSDate() 或 Date() 显示错误的时间

问题描述 投票:0回答:3

当我尝试记录当前日期时:

print(NSDate())

print(Date()) 

(在 Swift 3 中)

或者任何日期对象,它显示错误的时间。比如现在大约是16点12分,但是上面显示的是

2016-10-08 20:11:40 +0000

我的约会时区是否错误?如何确定我的日期以获得正确的时区?

这是为什么,如何解决?如何在打印语句或调试器中轻松显示本地时区的任意日期?

(请注意,这个问题是一个“铃声”,以便我可以提供一个简单的 Swift 3/Swift 2 Date/NSDate 扩展,让您轻松显示本地时区的任何日期对象。

swift date nsdate
3个回答
24
投票

NSDate(或 Swift ≥ V3 中的 Date)没有时区。它记录了世界各地的时间瞬间。

在内部,日期对象记录自“纪元日期”以来的秒数,即格林威治标准时间(又名 UTC)中的 2001 年 1 月 1 日午夜。

我们通常会想到当地时区的日期。

如果您使用

记录日期
print(NSDate()) 

系统显示当前日期,但以 UTC/格林威治标准时间表示。因此,时间看起来正确的唯一地方就是该时区。

如果发出调试器命令,您会在调试器中遇到相同的问题

e NSDate()

这是一种痛苦。我个人希望 iOS/Mac OS 能够使用用户当前时区显示日期,但他们没有。

编辑#2:

对我之前使用本地化字符串的改进是创建

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:.none)

编辑:

我刚刚发现

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 版本放入您的所有项目中。

然后您就可以使用

斯威夫特2:

print(NSDate().localDateString())

斯威夫特3:

print(Date().localDateString())

4
投票

纠正时区日期的一个简单方法是使用

TimeZone.current.secondsFromGMT()

对于本地时间戳值来说,类似这样:

let currentLocalTimestamp = (Int(Date().timeIntervalSince1970) + TimeZone.current.secondsFromGMT())


0
投票

2023 年的答案更简单

// for the standard Apple look
let nowish = DateFormatter.localizedString(
                    from: Date(), dateStyle: .short, timeStyle: .short)
print(nowish)

结果 10/28/23

// for a custom format. you need all four lines of code, in this order:
let ft = DateFormatter()
ft.locale = Locale.current
ft.setLocalizedDateFormatFromTemplate("MM/dd/yyyy")
let nowish = ft.string(from: Date())
print(nowish)

结果 2023 年 10 月 28 日

请注意一个公式具有“localizedString”,另一个公式具有“string”。

您经常需要“从现在起十天、去年等”的方法。在这种情况下,只需使用

Calendar.current

 let nextYr = Calendar.current.date(byAdding: .year, value: 2, to: Date())!

然后只需在上述公式中使用

nextYr
代替
Date()

© www.soinside.com 2019 - 2024. All rights reserved.