简体   繁体   English

SwiftDate字符串解析返回的结果少了1天

[英]SwiftDate string parsing returned 1 day less result

I'm trying to parse a date string into a Date class. 我正在尝试将日期字符串解析为Date类。 I'm using SwiftDate for this. 我为此使用SwiftDate。 But when I tried to parse it, it returned 1 day less than the value in the string. 但是,当我尝试解析它时,它返回的时间比字符串中的值少1天。 Here are some examples: 这里有些例子:

let birthString = "1996-10-08"
self.birthday = Date(birthString, format: "yyyy-MM-dd", region: Region.current) 
//Result: self.birthday = 1996-10-07 17:00:00 UTC 

let expiredString = "2019-09-30"
self.membershipExpiredDate = Date(expiredString, format: "yyyy-MM-dd", region: Region.current) 
//Result: self.membershipExpiredDate = 2019-09-29 17:00:00 UTC

How do I fix this error? 如何解决此错误? Thanks. 谢谢。

SwiftDate uses regions, and you are setting the region to Region.current . SwiftDate使用区域,并且您将区域设置为Region.current The date is correct and you can check that using the .timeIntervalSince1970 property. 日期是正确的,您可以使用.timeIntervalSince1970属性进行检查。 What happens is that when you print the date (which is actually just a TimeInterval, aka Double, a specific point in time, independent of any calendar or time zone ), it's printing its description property, with the default time zone UTC (You can see it in your output 17:00:00 UTC ). 发生的情况是,当您打印日期(实际上只是一个TimeInterval,又称Double,一个特定的时间点,与任何日历或时区无关 )时,它将打印其description属性,并使用默认时区UTC (您可以在您的输出17:00:00 UTC看到它)。

To print a date using your current locale, use this instance method: 要使用当前语言环境打印日期,请使用以下实例方法:

print(date.description(with: Locale.current))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM