Converting timestamp string with T and Z characters to NSDate [duplicate] - ios

This question already has answers here:
Is there a simple way of converting an ISO8601 timestamp to a formatted NSDate?
(8 answers)
Closed 8 years ago.
I don't know how to convert this string to NSDate.
Date is in this format "2012-10-21T07:00:00Z"
I guess the problem is that I have no clue what those T and Z mean in the string :)
NSDateFormatter *formatter = [[NSDateFormatter alloc]init];
[formatter setDateFormat:#"yyyy-MM-ddTHH:mm:ssZ"];
NSDate *dateStart = [formatter dateFromString:#"2012-10-21T07:00:00Z"];
// format that I want
[formatter setDateFormat:#"yyyy-MM-dd HH:mm"];
NSLog(#"date: %#", [formatter stringFromDate:dateStart]);
all dates are in that format: 2014-09-12T03:46:25Z

That is an ISO date. The Z stands for Zulu but means UTC time.
Try:
[formatter setDateFormat:#"yyyy-MM-dd'T'HH:mm:ssZZZZZ"];
To feed your initial nsdate.

Related

Format NSDate from string [duplicate]

This question already has answers here:
Getting date from [NSDate date] off by a few hours
(3 answers)
Closed 8 years ago.
I have some trouble to convert this string :
#"15/08/2014 08:30"
Into an NSDate, here my code :
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setLocale:[NSLocale currentLocale]];
[formatter setTimeZone:[NSTimeZone localTimeZone]];
[formatter setDateFormat:#"dd/MM/yyyy HH:mm"];
When I print out [formatter dateFromString:fullDatesString]; (fullDatesString is my string) the result is :
2014-08-15 06:30:00 +0000
Why my time is less than 2 hours between my string and my NSDate object ?
When you print the description of the NSDate, it prints in in GMT (+0000) regardless of what timezone you or the NSDate is in.
If you convert that time to your timezone (GMT + 2 I assume), then the time is correct.

Getting one date less [duplicate]

This question already has answers here:
NSDate Format outputting wrong date
(5 answers)
Closed 8 years ago.
I don't why i'm getting one date less, when I'm converting a string from a date, i'm getting one date less, e.g. when i'm converting 18/06/2014, i'm getting 2014-06-17, Any idea why this problem, my codes are:
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"dd/MM/yyyy"];
NSDate *date = [dateFormatter dateFromString:#"18/06/2014"];
This is what I'm getting wholly from the log: 2014-06-17 20:00:00 +0000
You will have to take the timezone into account. Your current timezone seems to be ahead of GMT. If you print the entire date with say a time stamp, then you will get the difference. So i suggest you add the timezone to the NSDateFormatter
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"dd/MM/yyyy"];
dateFormatter.timeZone = [NSTimeZone timeZoneForSecondsFromGMT:0];
NSDate *date = [dateFormatter dateFromString:#"18/06/2014"];
NSLog(#"Date : %#", date);

Getting a date from string objective C [duplicate]

This question already has an answer here:
MagicalRecord date parsing
(1 answer)
Closed 8 years ago.
How can i get a date from the following date "2014-05-16T16:15:07+01:00"
I used NSDateFormatter as shown below but it didnt work. It gives null as a result.
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"yyyy-MM-dd'T'HH:mm:ss'Z'"];
[dateFormatter dateFromString:sDate];
Remove the single quotes around Z in your date format string:
NSString *sDate = #"2014-05-16T16:15:07+01:00";
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"yyyy-MM-dd'T'HH:mm:ssZZZZZ"]; // 5 Zs is technically correct here.
NSDate *result = [dateFormatter dateFromString:sDate];
NSLog(#"result: %#", result); //result: 2014-05-16 15:15:07 +0000
When you quote the Z, the date formatter expects a literal 'Z' character in the input string rather than the time zone as an offset from GMT, which is what your date string contains.

ios convert NSString into date? [duplicate]

This question already has answers here:
Converting a string to an NSDate
(6 answers)
Closed 9 years ago.
I have a string containing:
2013-10-29 18:50:18 +0000
How can i convert this into a date, but keeping the same date format?
I've tried:
NSString *str3 = [[NSString alloc] initWithFormat:#"%#", time];
NSDateFormatter *df = [[NSDateFormatter alloc] init];
[df setDateFormat:#"yyyy-MM-dd hh:mm:ss a"];
NSDate *myDate = [df dateFromString:str3];
But myDate returns (null)
Any ideas?
Try this format:
yyyy-MM-dd HH:mm:ss ZZZ
HH for hours, because you clearly have 24-hour clock
ZZZ for timezone, because you used a and that’s for AM/PM period
Full reference of Unicode Date Format Patterns.

Time format in specific format [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
format a NSDate to DDMMYYYY?
Want NSDateformatter for January,2000 or January 2000
I need date and time in following format. How can I get ..
Dec 17,2012 5:30 AM
You can always google for these kind of issues
Try to look at NSDateFormatter Class,
The format you are looking for is something like:
[dateFormatter setDateFormat:#"MMM dd,yyy hh:mm:ss a"]; //dateFormatter is an object of class NSDateFormatter
NSDate *selected_Date = //Your Date object here;
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"MMM dd,yyy hh:mm:ss a"];
NSString* strDateObj [NSString stringWithFormat:#"%#",[dateFormat stringFromDate:selected_Date]];
NSLog(#"Date %#",)

Resources