- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
当我尝试记录当前日期时:
print(NSDate())
或
print(Date())
(在 Swift 3 中)
或者任何日期对象,它显示错误的时间。比如现在大概是16点12分,但是上面显示的是
2016-10-08 20:11:40 +0000
我的约会时区是否错误?如何修正我的日期以获得正确的时区?
为什么会这样,如何解决?如何在打印语句或调试器中轻松显示本地时区的任意日期?
(请注意,这个问题是一个“铃声”,以便我可以提供一个简单的 Swift 3/Swift 2 Date/NSDate 扩展,让您轻松显示本地时区的任何日期对象。
最佳答案
NSDate(或 Swift ≥ V3 中的日期)没有时区。它记录了世界各地的时间瞬间。
在内部,日期对象记录自“纪元日期”以来的秒数,或 2001 年 1 月 1 日午夜 Greenwich Mean Time 中的秒数。 ,又名 UTC。
我们通常会考虑本地时区的日期。
如果您使用记录日期
print(NSDate())
系统显示当前日期,但以 UTC/格林威治标准时间表示。因此,时间看起来正确的唯一地方就是该时区。
如果您发出调试器命令,您会在调试器中遇到相同的问题
e NSDate()
这是一个痛苦。我个人希望 iOS/Mac OS 能够使用用户当前时区显示日期,但事实并非如此。
对我之前使用本地化字符串的改进是创建 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 :.无)
我刚刚发现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 版本放入您的所有项目中。
然后您可以使用
swift 2:
print(NSDate().localDateString())
swift 3:
print(Date().localDateString())
关于swift - NSDate() 或 Date() 显示错误的时间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54054019/
我的应用程序中有两个 UIDatePicker 控件,一个配置了 UIDatePickerModeDate,另一个配置了 UIDatePickerModeTime。 我想配置一个 NSDate 对象,
我可以通过此代码获取 iPhone 的当前日期 NSDate *currentDate = [NSDate date]; NSDateFormatter *dateFormatter1 = [[NSD
我在 Swift 中有一个模型,它有一个名为 expirationDate 的 NSDate 字段,我想根据当前时间计算过期前的剩余小时数日期。 有没有一种简单的方法可以利用现有的 NSDate 功能
我将我的 NSDate 存储到一个文件中,该文件采用您在 NSDate 上调用描述时获得的国际格式。 但是,我不知道如何从字符串格式返回到 NSDate 对象。 NSDateFormatter 似乎仅
我有兴趣创建一种方法来查找当前日期是否落在任何给定日期的特定时间之间。这是一个调度程序,所以我想找到当前时间正在发生什么事件。每天都有相同的时间集:820-900、900-940、940-1020 等
我有一个约会时间: Friday, July 8, 2016 at 6:30:00 PM India Standard Time 我想要这样的约会时间: Friday, July 8, 2016 at
我有一个 NSDate 对象。假设它代表“1-10-2011” NSDate *date = [df dateFromString:@"2011-10-01 00:00:00"]; 由于我所在的时区,
我从 Django API 获取 NSString 2012-08-17T10:56:45.508205 作为时间。 我正在尝试使用以下代码将该字符串转换为 NSDate 对象: NSDateForm
我在获取一系列 NSDates 与另一个 NSDates 范围相交的天数时遇到问题。 场景 1: 主要范围:3月8日-6月8日 检查范围:4月8日-5月8日 那么相交的天数就是4月8日-5月8日的天数
我知道如何获取两个 NSDate 之间的差异,如下所示 NSTimeInterval timeInterval = [[NSDate date] timeIntervalSinceDate:anyPr
我需要根据另一个 NSDate 获取一天中特定时间的 NSDate。例如,如果我有一个代表 2015 年 5 月 10 日已知时间的 NSDate,我需要能够获得一个代表 2015 年 5 月 10
升级到1.2后出现编译错误: NSDate?! is not convertible to NSDate 代码: let dateCreated = photoCommentObjects[index
这是我的代码。但是一个错误说,不能用类型的参数列表调用 dateComponent... let fromDate = NSDate(timeIntervalSince1970: TimeInter
给定一个引用日期,我需要找到下面的“星期三”。 需要说明的是,如果引用日期是星期二,那么它应该返回同一周的星期三。 如果引用日期是星期三或更晚,则需要返回从下周开始的下一个星期三。 重要的是它不会返回
我有 NSDate* day1 和 NSDate* day2。我如何通过只复制第 1 天的年/月/日和第 2 天的小时/分钟/秒来制作 NSDate* day3? 最佳答案 unsigned unit
我有两个 NSDate 对象,我想要两者之间的差异,结果应该再次是一个 NSDate 对象。知道如何实现这一目标吗? 在这里,我试图解决一个独特的问题,我必须找出耗时,然后本地化耗时。如果我在 NSD
我试图强制用户使用日期选择器选择 future 的日期。我显然使用了compare:方法,但是,当我执行以下代码时,即使它与[NSDate date]相同的日期,它也会告诉执行if语句。这是我的代码:
我有两个用户选择的日期:开始日期和结束日期。它们是 NSDate 实例,我必须将它们作为 NSNumber 的参数发送。如何以秒为单位将它们转换为 NSNumber? 最佳答案 使用下面的代码: N
我对将 NSTimeInterval 转换为 NSDate 感到困惑,我有两个日期要比较,一个来自服务器,另一个在本地, 在服务器案例中,我以时间间隔格式获取日期,因此我也必须对本地数据执行相同的操作
我有一个从服务器获取的字符串 "yyyy-MM-dd'T'HH:mm:ss.SSS'Z"我通过这个 formate 将这个字符串转换成 NSDate。 class func convertUTCDat
我是一名优秀的程序员,十分优秀!