找到你要的答案

Q:Formatting date with Swift

Q:格式化日期与SWIFT

I am trying to Convert a String into Date using Date Formatter .

var dateFormatter = NSDateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd’T’HH:mm:ss’Z’"
var AvailabletoDateTime = dateFormatter.dateFromString("2015-07-16’T’08:00:00’Z’")

But the AvailabletoDateTime is Returning nil . Can Someone please Help me ,and let me know if i miss Anything Here .

我试图将字符串转换成日期使用日期格式。

var dateFormatter = NSDateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd’T’HH:mm:ss’Z’"
var AvailabletoDateTime = dateFormatter.dateFromString("2015-07-16’T’08:00:00’Z’")

But the AvailabletoDateTime is Returning nil . Can Someone please Help me ,and let me know if i miss Anything Here .

answer1: 回答1:

Try like this:

let dateFormatter = NSDateFormatter()
dateFormatter.calendar = NSCalendar(calendarIdentifier: NSCalendarIdentifierGregorian)
dateFormatter.locale = NSLocale(localeIdentifier: "en_US_POSIX")
dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ssZ"
let availabletoDateTime = dateFormatter.dateFromString("2015-07-16T08:00:00Z")

这样尝试:

let dateFormatter = NSDateFormatter()
dateFormatter.calendar = NSCalendar(calendarIdentifier: NSCalendarIdentifierGregorian)
dateFormatter.locale = NSLocale(localeIdentifier: "en_US_POSIX")
dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ssZ"
let availabletoDateTime = dateFormatter.dateFromString("2015-07-16T08:00:00Z")
answer2: 回答2:

This looks like you're trying to use an ISO 8601/RFC 3339 date. Usually the date string will not include any quotes. It will be: something like 2015-07-16T08:00:00Z. FYI, the Z stands for GMT/UTC timezone (as opposed to your local timezone).

So, in Swift 3:

let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ssZZZZZ"
dateFormatter.locale = Locale(localeIdentifier: "en_US_POSIX")
dateFormatter.timeZone = TimeZone(secondsFromGMT: 0)
let availabletoDateTime = dateFormatter.date(from: "2015-07-16T08:00:00Z")

Or use the new ISO8601DateFormatter:

let dateFormatter = ISO8601DateFormatter()
let availabletoDateTime = dateFormatter.date(from: "2015-07-16T08:00:00Z")

In Swift 3, use this ISO8601DateFormatter, if you can, but recognize that it doesn't provide as much control (e.g. whether to include milliseconds or not). But in this case, it's sufficient.

FYI, in Swift 2:

let dateFormatter = NSDateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ssZZZZZ"
dateFormatter.locale = NSLocale(localeIdentifier: "en_US_POSIX")
dateFormatter.timeZone = NSTimeZone(forSecondsFromGMT: 0)
let availabletoDateTime = dateFormatter.dateFromString("2015-07-16T08:00:00Z")

Note, I'm (a) using ZZZZZ rather than Z in my format string; and (b) explicitly setting the timeZone. I do this for my formatter can not only successfully parse the string 2015-07-16T08:00:00Z into a NSDate object, but that it can also be used to go the other direction, converting a NSDate object into a string in the format like 2015-07-16T08:00:00Z.

Also note I'm using ' character, not the ’ character. Nor do I need to set the calendar, as locale of en_US_POSIX is all you need.

For more information, see Technical Q&A 1480.

这就像你试图使用ISO 8601 / RFC 3339日期。通常日期字符串不包括任何引号。这将是:像2015-07-16t08:00:00z。仅供参考,Z代表UTC时区GMT /(相对于本地时区)。

所以,在斯威夫特3:

let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ssZZZZZ"
dateFormatter.locale = Locale(localeIdentifier: "en_US_POSIX")
dateFormatter.timeZone = TimeZone(secondsFromGMT: 0)
let availabletoDateTime = dateFormatter.date(from: "2015-07-16T08:00:00Z")

或使用新的iso8601dateformatter:

let dateFormatter = ISO8601DateFormatter()
let availabletoDateTime = dateFormatter.date(from: "2015-07-16T08:00:00Z")

在快3,使用此iso8601dateformatter,如果可以,但承认它不提供尽可能多的控制(如是否包括毫秒或不)。但在这种情况下,就足够了。

仅供参考,斯威夫特2:

let dateFormatter = NSDateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ssZZZZZ"
dateFormatter.locale = NSLocale(localeIdentifier: "en_US_POSIX")
dateFormatter.timeZone = NSTimeZone(forSecondsFromGMT: 0)
let availabletoDateTime = dateFormatter.dateFromString("2015-07-16T08:00:00Z")

请注意,我是(一)使用zzzzz而不是Z在我的格式字符串;和(b)明确设置时区。我做这些是为了我的格式化程序不仅能够成功地解析字符串2015-07-16t08:00:00z成NSDate对象,但它也可以被用来去另一个方向,转换一个NSDate对象为字符串格式如2015-07-16t08:00:00z。

还要注意我使用的是字符,不是字符。我也不需要设置日历,为现场en_us_posix就是你全部的需要。

欲了解更多信息,请参阅技术问答1480。

swift  nsdateformatter