如何获得当前的EST时间IOS Swift [英] How to get Current EST time IOS Swift

查看:134
本文介绍了如何获得当前的EST时间IOS Swift的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在我的应用中比较两个日期,并且我有一个EST日期与当前日期进行比较,但是 let today = NSDate()以UTC格式返回日期,我怎样才能获得当前的EST时间或转换为EST?

I need to compare two dates in my app and i have a EST date to compare with current date , but let today = NSDate() returns the date in UTC , how can i get current EST time or convert to EST ?

推荐答案

NSDate 以绝对时刻存储时间即使用比较 NSDate 它将尊重时区(因为时区从未存储过)。您可以将 NSDate 视为纪元时间,该时间始终从1970年1月1日星期四00:00:00协调世界时(UTC)开始计算

The NSDate store the time in absolute moment i.e. No matter in what time zone your date was created when you use compare method of NSDate it will respect the time zone (because time zone was never stored). You can think of NSDate as epoch time which is always calculated from 00:00:00 Coordinated Universal Time (UTC), Thursday, 1 January 1970

我不知道 NSDate 的实现可能会将日期存储在 UTC 内部。

I don't know about the implementation of NSDate may be it store the date in UTC internally.

我们的实现( NSDateFormatter )将其格式更改为我们的要求。

It's our implementation (NSDateFormatter) that changes it's format to our requirement.

因此,无论您如何使用比较 NSCalendar ,无论您如何创建日期他们会比较你的日期

So whether you use compare or NSCalendar regardless how you created your date they will compare you date

例如我当地的时区是IST,现在是2016-10-21 12:10:00

e.g. My local time zone is IST and it's 2016-10-21 12:10:00 here right now

在美国东部时间2016-10-21 02:40:00,

在PST它是2016-10-20 23:40:00现在

In EST it's 2016-10-21 02:40:00,
In PST it's 2016-10-20 23:40:00 right now

所以这个代码

let today = NSDate()
print("\(today)")


let formatter = DateFormatter()
formatter.dateFormat = "yyyy-MM-dd HH:mm:ss"
formatter.timeZone = TimeZone(abbreviation: "EST")
let estDate = formatter.date(from: "2016-10-21 02:40:00")
print("\(estDate!)")


formatter.timeZone = TimeZone(abbreviation: "PST")
let pstDate = formatter.date(from: "2016-10-20 23:40:00")
print("\(pstDate!)")


formatter.timeZone = TimeZone(abbreviation: "IST")
let istDate = formatter.date(from: "2016-10-21 12:10:00")
print("\(istDate!)")

会打印同一时间,因为它处于同一时刻

Will print the same time because it's the same moment everywhere

2016-10-21 06:40:00 +0000
2016-10-21 06:40:00 +0000
2016-10-21 06:40:00 +0000
2016-10-21 06:40:00 +0000

这篇关于如何获得当前的EST时间IOS Swift的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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