Jargen89
Jargen89

Reputation: 480

NSDate, Can't get proper date format

This is the timestamp I am trying to get my app to read: Thu, 03 Mar 2016 16:06:42 GMT

This is the closest I have, but haven't had any success:

[df setDateFormat:@"EEE, dd MMM YYYY HH:mm:ss z"];

EDIT: My current format prints-to/reads: Tue, 02 M02 2016 11:36:57 GMT-5

is there anything I'm missing?

Final Edit:

I should have just posted all my code...

    [df setLocale:[NSLocale systemLocale]];

This was messing things up for me.

Upvotes: 0

Views: 85

Answers (2)

Dominic Krause
Dominic Krause

Reputation: 112

I tried to use your date format and convert it to a nsdate and back to a string. It should work with the format string you used.

let time = "Thu, 03 Mar 2016 16:06:42 GMT"
let dateformat = "EEE, dd MMM yyyy HH:mm:ss z"

var dateformatter = NSDateFormatter()
dateformatter.dateFormat = dateformat
dateformatter.timeZone = NSTimeZone(forSecondsFromGMT: 0)

var date = dateformatter.dateFromString(time)

var newTime = dateformatter.stringFromDate(date!)
// newTime = "Thu, 03 Mar 2016 16:06:42 GMT"

Edit: I changed the dateformat to use lowercase ys for the year as suggested by Devster101. This is the correct format according to this spezification which is used since iOS 7

Upvotes: 1

Devster101
Devster101

Reputation: 578

I believe your year is incorrect. Change your dateformat to

[df setDateFormat:@"EEE, dd MMM yyyy HH:mm:ss z"];

See documentation on formatter dates here

Upvotes: 2

Related Questions