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

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

问题描述

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

  print(NSDate())

  print(Date )

(在Swift 3中)



或任何日期对象,它显示错误的时间。例如现在大概是16:12,但是上面显示的是

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

我的日期在错误的时区吗?如何修正日期以获得正确的时区?



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



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

解决方案

NSDate(或Swift 3中的日期)没有时区,它记录了世界各地的时间。



在内部,日期对象记录纪元日期以来的秒数,或2001年1月1日的午夜,在格林威治标准时间,aka UTC。



我们通常在本地时区考虑日期。



如果您登录日期使用

  print(NSDate())

系统显示当前日期,但以UTC /格林威治标准时间表示,所以只有在这个时区才会看到正确的时间。



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

  e NSDate()

是一种痛苦。我个人希望iOS / Mac OS将使用用户当前时区显示日期,但不会。



编辑:



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

  class func localizedString(from date:Date,dateStyle dstyle:DateFormatter.Style,timeStyle tstyle:DateFormatter。风格) - > String 

所以你只需使用

  let now = Date()
print(DateFormatter.localizedString(
from:now,
dateStyle:.short,
timeStyle:。简单))

你几乎可以忽略下面的一切。






我已经创建了NSDate类的一个类别(在swift 3中的Date),该类具有在用户的本地时区显示日期的localDateString方法。 >

以下是Swift 3表单中的类别:(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形式:

 扩展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())


When I try to log the current date:

print(NSDate())

or

print(Date()) 

(in Swift 3)

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?

(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 (or Date in Swift 3) does not have a time zone. It records an instant in time all over the world.

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.

If you log a date using

print(NSDate()) 

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

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

EDIT:

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

So you'd simply use

let now = Date()
print (DateFormatter.localizedString(
  from: now, 
  dateStyle: .short, 
  timeStyle: .short))

You can pretty much ignore everything below.


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.

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

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

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

You can then use

Swift 2:

print(NSDate().localDateString())

Swift 3:

print(Date().localDateString())

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

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