从NSDate对象获取UTC时间和本地时间 [英] Get UTC time and local time from NSDate object

查看:199
本文介绍了从NSDate对象获取UTC时间和本地时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在objective-c中,以下代码使用 date API生成UTC日期时间信息。

In objective-c, the following code results in the UTC date time information using the date API.

NSDate *currentUTCDate = [NSDate date]

然而在Swift中,

let date = NSDate.date()

产生当地日期和时间。

我有两个问题:


  1. 如何在 NSDate中获取UTC时间和当地时间(日期给出当地时间) objects。

  2. 如何从 NSDate 对象获得秒数的精度。

  1. How can I get UTC time and local time (well date gives local time) in NSDate objects.
  2. How can I get precision for seconds from the NSDate object.

编辑1:感谢所有输入,但我不是在寻找 NSDateFormatter 对象或字符串值。我只是在寻找NSDate对象(但是我们将它们煮熟,但这是要求)。
见第1点。

EDIT 1: Thanks for all the inputs but I am not looking for NSDateFormatter objects or string values. I am simply looking for NSDate objects (however we cook them up but that's the requirement).
See point 1.

推荐答案

文档说明 date 方法返回一个新的日期设置为当前日期和时间,无论使用何种语言。

The documentation says that the date method returns a new date set to the current date and time regardless of the language used.

该问题可能位于使用 NSDateFormatter 显示日期的地方。 NSDate 只是时间线上的一个点。在谈论 NSDate 时,没有时区。我做了一个测试。

The issue probably sits somewhere where you present the date using NSDateFormatter. NSDate is just a point on a time line. There is no time zones when talking about NSDate. I made a test.

print(NSDate())

输出: 2014-07-23 17:56:45 +0000

NSLog(@"%@", [NSDate date]);

输出: 2014-07-23 17:58:15 +0000

结果 - 没有区别。

这篇关于从NSDate对象获取UTC时间和本地时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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