Getting a date from string objective C [duplicate] - ios

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.

Related

Format NSString 2015-05-24T20:10:00.000Z to M/d, h:mma (5/24, 8:00PM) [duplicate]

This question already has answers here:
Converting an ISO 8601 timestamp into an NSDate: How does one deal with the UTC time offset?
(6 answers)
Closed 7 years ago.
I have an NSString #"2015-05-24T20:10:00.000Z", and I am using an NSDateFormatter with the date format #"yyyy-MM-dd'T'HH:mm:ssZ", but it always returns nil.
I am using the following code, but the output is always (null).
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
// NSString *input = #"2013-05-08T19:03:53+00:00";
[dateFormat setDateFormat:#"yyyy-MM-dd'T'HH:mm:ssZ"]; //iso 8601 format
NSDate *date = [dateFormat dateFromString:[dictScheduleData valueForKey:#"scheduledOn"]]; // coming from the server 2015-05-24T20:10:00.000Z
NSLog(#"Date output: %#", date);
Try this dateFormat
#"yyyy-MM-dd'T'HH:mm:ss.SSSZ"
You have to match the string's date format. Use NSDateFormatter, then set the date format to match the string's date format. Then use dateFromString method.
to convert it to (5/24, 8:00PM), you can change NSDateFormatter's dateFormat to "M/d, h:mma", then use NSDateFormatter's method stringFromDate (the date you previously got, which should not be nil if done correctly). Then put that string in UILabel's text.
NSDate *currentTime = [NSDate date];
NSDateFormatter *timeFormatter = [[NSDateFormatter alloc] init];
[timeFormatter setDateFormat:#"yyyy-MM-dd HH:mm:ss"];
[timeFormatter setLocale:[NSLocale currentLocale]];
NSString *DateString = [timeFormatter stringFromDate:currentTime];

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

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.

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);

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.

Date Format for String [duplicate]

This question already has answers here:
NSDateFormatter won't format strange date string
(3 answers)
Closed 9 years ago.
I have a string of the form "2013-05-12T15:38:20+00:00" but I can't work out why my date formatter won't convert it to an NSDate object. I have tried using "yyyy-MM-ddTHH:mm:ss+zz:zz" and a bunch of variations on it but no luck.
What would be the correct date format for this string?
Thanks
The correct date format for this string is "yyyy-MM-dd'T'HH:mm:ssz"
Thanks #Anupdas for pointing me to a similar question.
Use this format
NSDateFormatter *form = [[NSDateFormatter alloc] init];
[form setDateFormat:#"yyyy-MM-dd'T'HH:mm:sszzz"];
NSLog(#"notDate =====> %#",[form dateFromString:#"2013-05-12T15:38:20+00:00"]);
NSDate *notDate = [form dateFromString:[#"" stringByAppendingFormat:#"2013-05-12T15:38:20+00:00"]];
NSLog(#"notDate =====> %#",[form dateFromString:#"2013-05-12T15:38:20+00:00"]);
I hope it helps you
Try this code
your string
NSDateFormatter* dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"yyyy-MM-ddTHH:mm:ss+zz:zz"];
NSDate* date = [dateFormatter dateFromString:dateString];
Conversion of the NSString to a NSDate
Now here you change any format as you want
[dateFormatter setDateFormat:#"dddd - MMMM dd,yyy"];
NSString* myNiceLookingDate = [dateFormatter stringFromDate:date];
[dateFormatter release];

Resources