NSDate() 或 Date() 显示错误的时间 [英] NSDate() or Date() shows the wrong time

查看:29
本文介绍了NSDate() 或 Date() 显示错误的时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

When I try to log the current date:

print(NSDate())

print(Date()) 

(在 Swift 3 中)

(in Swift 3)

或任何日期对象,它显示错误的时间.比如现在是16:12左右,但是上面显示的是

Or any date object, it shows the wrong time. For example, it's about 16:12 now, but the above displayed

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

我的约会对象是否在错误的时区?如何确定我的日期以使用正确的时区?

Is my date in the wrong time zone? How do I fix my date to have the correct time zone?

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

Why is that, and how to I fix it? How do I easily display an arbitrary date in my local time zone, either in print statements or in the debugger?

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

(Note that this question is a "ringer" so that I can provide a simple Swift 3/Swift 2 Date/NSDate extension that lets you easily display any date object in your local time zone.

推荐答案

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

NSDate (or Date in Swift ≥ V3) does not have a time zone. It records an instant in time all over the world.

在内部,日期对象记录自纪元日期"或 格林威治标准时间,又名UTC.

Internally, date objects record the number of seconds since the "epoch date", or Midnight on January 1, 2001 in Greenwich Mean Time, a.k.a UTC.

我们通常会想到本地时区的日期.

We normally think of dates in our local time zone.

如果您使用

print(NSDate()) 

系统显示当前日期,但以UTC/格林威治标准时间表示.所以时间看起来正确的唯一地方是在那个时区.

The system displays the current date, but it expresses it in UTC/Greenwich Mean Time. So the only place the time will look correct is in that time zone.

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

You get the same issue in the debugger if you issue the debugger command

e NSDate()

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

This is a pain. I personally wish iOS/Mac OS would display dates using the user's current time zone, but they don't.

对我之前使用的本地化字符串的改进使其更易于使用的是创建对 Date 类的扩展:

An improvement on my previous use of localized string that makes it a little easier to use is to create an extension to the Date class:

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)

That way you can just use an expression like Date().localString(), or if you want to only print the time, you can use Date().localString(dateStyle:.none)

我刚刚发现NSDateFormatter(Swift 3 中的DateFormatter)有一个类方法localizedString.这做我下面的扩展所做的,但更简单和干净.这是声明:

I just discovered that NSDateFormatter (DateFormatter in Swift 3) has a class method localizedString. That does what my extension below does, but more simply and cleanly. Here is the declaration:

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))

您几乎可以忽略以下所有内容.

You can pretty much ignore everything below.

我创建了一个 NSDate 类的类别(Swift 3 中的 Date),它有一个 localDateString 方法,可以在用户的​​本地时区显示日期.

I have created a category of the NSDate class (Date in swift 3) that has a method localDateString that displays a date in the user's local time zone.

这里是 Swift 3 形式的类别:(filename Date_displayString.swift)

Here is the category in Swift 3 form: (filename 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 形式:

And in Swift 2 form:

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

(如果您更喜欢不同的日期格式,可以很容易地修改日期格式化程序使用的格式.在您需要的任何时区显示日期和时间也很简单.)

(If you prefer a different date format it's pretty easy to modify the format used by the date formatters. It's also straightforward to display the date and time in any timezone you need.)

我建议在您的所有项目中放置此文件的适当 Swift 2/Swift 3 版本.

I would suggest putting the appropriate Swift 2/Swift 3 version of this file in all of your projects.

然后你可以使用

斯威夫特 2:

print(NSDate().localDateString())

斯威夫特 3:

print(Date().localDateString())

这篇关于NSDate() 或 Date() 显示错误的时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆