10

I would like to format a date string into a NSDate object, which doesn't sound like a big thing.

The point is, that the date string contains a dot in the timezone value instead of a plus or something else. A date looks like this:

2017-06-04T16:00:00.000Z

I tried format strings like

yyyy-MM-dd'T'HH:mm:ss.ZZZZ
yyyy-MM-dd'T'HH:mm:ss.ZZZ
yyyy-MM-dd'T'HH:mm:ss.Z

Of course I've also checked it on nsdateformatter.com, which works but in xCode the NSDate is always nil.

3
  • 2
    yyyy-MM-dd'T'HH:mm:ss.SSS'Z'??
    – Larme
    May 26, 2017 at 18:52
  • 1
    @Larme Do not quote the Z. That's the timezone. When you quote that, the string gets interpreted in local time unless a specific timezone is set on the date formatter. If you don't quote the Z there is no need to set a specific timezone on the formatter and the string gets interpreted properly.
    – rmaddy
    May 27, 2017 at 1:03
  • @rmaddy what the meaning of Z actually? do you have any reference that I can read into? Dec 30, 2020 at 23:48

3 Answers 3

22

This work for me

var str = "2017-06-04T16:00:00.000Z"
let formato = DateFormatter()
formato.dateFormat = "yyyy-MM-dd'T'HH:mm:ss.SSSZ"
formato.timeZone = NSTimeZone(name: "UTC")! as TimeZone
formato.formatterBehavior = .default
var data = formato.date(from: str)
1
  • There is no need to set the timezone on the formatter. The Z in the date string being parsed tells the date formatter what timezone the string represents.
    – rmaddy
    May 27, 2017 at 1:03
2

In Objective-C

NSString *strValue = @"2017-06-04T16:00:00.000Z";
NSString *dateFormat = @"yyyy-MM-dd'T'HH:mm:ss.SSSZ"

// Or this if you like get in local time
NSString *dateFormat = @"yyyy-MM-dd'T'HH:mm:ss.SSS'Z'"

NSDateFormatter *dateFmtr = [[NSDateFormatter alloc] init];
[dateFmtr setDateFormat: dateFormat];

// You get a NSDate object
NSDate *dateValue = [dateFmtr dateFromString: strValue];

// Or NSString object
NSString *dateValue = [dateFmtr stringFromDate: dateValue];

Other variants for different strings formats.

// 2018-02-28T16:38:33.6873197-05:00
// If you have this string format you can use
NSString *strDate1 = @"yyyy-MM-dd'T'HH:mm:ss.SSSSSSZ";

For more information with complex formats you can see Patterns Date Format Patterns and the Apple's official documentation Preset Date and Time Styles and Date Formatters

0

I faced the same problem of always getting nils & as a solution you can eliminate the .ZZZZ part from the date String

let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd HH:mm:ss"
dateFormatter.timeZone = TimeZone(secondsFromGMT: 0)

let dateString = openningTimeString.components(separatedBy: ".")[0]

let myDate = dateFormatter.date(from: dateString)
1
  • A much, much better solution to breaking up the original date string yourself is to simply use the proper date format.
    – rmaddy
    May 27, 2017 at 1:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.