How to parse an ISO8601 date [duplicate] - iphone

This question already has answers here:
How to parse a date string into an NSDate object in iOS?
(7 answers)
Closed 9 years ago.
I got string of date regarding this format:
2011-12-29T09:09:06-0500
How can I convert this into a date object?

Try this solution
NSString *dateString = #"2011-12-29T09:09:06-0500";
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"yyyy-MM-dd'T'HH:mm:ssZ"];
NSDate *dateFromString = [dateFormatter dateFromString:dateString];
[dateFormatter release];
NSLog(#" dateFromString %#",dateFromString);

NSString *date = [[NSDate date] description];
[NSDateFormatter setDefaultFormatterBehavior:NSDateFormatterBehavior10_4];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"yyyy'-'MM'-'dd'T'HH':'mm':'ss'Z'"];
NSDate *dateFromString = [[NSDate alloc] init];
dateFromString = [dateFormatter dateFromString:<NSString containing date>];

Related

How to Change Date and Time Format? [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How to change the date format in iphone sdk
Actually I'm getting a string in 24 Hours time format. I want to display 12 hours time format.
Here's my input :
2012-10-19T04:10:00
2012-10-06T14:00:00
I would like to have the following format :
10-19-2012 4:10 AM
10-06-2012 2:00 PM
:use the NSDateFormatter to change the date
Here Your input:
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"yyyy-MM-dd'T'HH:mm:ss"];
NSDate *Yourcurrenttime = [dateFormatter dateFromString:Yourstring];
And date converted to the String ..
NSDateFormatter *dateFormatterNew = [[NSDateFormatter alloc] init];
[dateFormatterNew setDateFormat:#"mm-dd-yyyy hh:mm a"];
NSString *stringForNewDate = [dateFormatterNew stringFromDate:Yourcurrenttime];
Use NSDateFormatter with format type dd-MM-yyyy hh:mm:ss a.
Hope this helps you..
NSDate *currentDate = [NSDate date];
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:#"dd-MM-yyyy hh:mm a"]; //You can set different formats for the date
NSLog(#"Current Date %#",[formatter getStringFromDate:currentDate"]);
[formatter release];
In most cases, you’d just need to use the default styles defined in NSDateFormatterStyle.
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateStyle:NSDateFormatterMediumStyle];
[dateFormatter setTimeStyle:NSDateFormatterShortStyle];
NSLog(#"%#", [dateFormatter stringFromDate:[NSDate date]]);
[dateFormatter release];
// Output: Dec 2, 2008 3:58 PM
also you can get output from coding with DateFormatter like bellow..
NSDateFormatter *dateFormatter = [[[NSDateFormatter alloc] init] autorelease];
[dateFormatter setDateFormat:#"MM-dd-yyy HH:mm:s a"];
NSString *convertedString = [dateFormatter stringFromDate:[NSDate date]];
NSLog(#"Converted String : %#",convertedString);
:)

Accurate date formate and compare date in iphone dev?

I am new in iphone, i want to change string into accurate date-formate which i couldn't done this for last 2 hrs. I have string like(17:30 18 Oct) and want to convert this in accurate formate and also compare this date to tomorrow if this in tomorrow then display just 18 Oct and if it on today just display time 17:30, any help please.
NSString to NSDate conversion
NSString *dateString = #"6:30 18 Oct";
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"hh:mm dd/MMM"];
NSDate *dateFromString = [[NSDate alloc] init];
dateFromString = [dateFormatter dateFromString:dateString];
[dateFormatter release];
Try this
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"hh:mma dd/MMM"];
NSString *dateString = [dateFormat stringFromDate:today];
NSLog(#"date: %#", dateString);
[dateFormat release];

Accurate Date formate for hours time and date month in iphone? [duplicate]

I am new in iphone, i want to change string into accurate date-formate which i couldn't done this for last 2 hrs. I have string like(17:30 18 Oct) and want to convert this in accurate formate and also compare this date to tomorrow if this in tomorrow then display just 18 Oct and if it on today just display time 17:30, any help please.
NSString to NSDate conversion
NSString *dateString = #"6:30 18 Oct";
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"hh:mm dd/MMM"];
NSDate *dateFromString = [[NSDate alloc] init];
dateFromString = [dateFormatter dateFromString:dateString];
[dateFormatter release];
Try this
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"hh:mma dd/MMM"];
NSString *dateString = [dateFormat stringFromDate:today];
NSLog(#"date: %#", dateString);
[dateFormat release];

iPhone SDK Date Format [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
How to change format of date/time?
In my program I'm making an API call for getting a date, but the received date has an incorrect format. It looks like 2011-07-06 00:00:00. How I can change it to 6-july-2011?
You try my code that will help you because I run successfully.
NSString *dateStr = #"2011-07-06 00:00:00";
NSDateFormatter *dtF = [[NSDateFormatter alloc] init];
[dtF setDateFormat:#"yyyy-MM-dd hh:mm:ss"];
NSDate *d = [dtF dateFromString:dateStr];
NSDateFormatter *dateFormatStr = [[NSDateFormatter alloc] init];
[dateFormatStr setDateFormat:#"d-MMM-yyyy"];
NSString *strDate = [dateFormatStr stringFromDate:d];
NSLog(#"%#",strDate);
try this code..
NSDateFormatter *formatter = [[[NSDateFormatter alloc] init] autorelease];
[formatter setDateFormat:#"dd-MMM-YYYY"];
NSString *dateStr = [formatter stringFromDate:[NSDate date]];

How change the date format which is stored in string? Objective c [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
how to convert datetime format in NSString?
I have stored date in string from json parsing. The format of date is 2011-1-24. Now i want to convert into MM-dd-YYYY format. For that i am using this code
NSString *stringDate =[NSString stringWithFormat:#"%#",[list_date objectAtIndex:indexPath.row]];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"MM-dd-yyyy"];
NSDate *date = [dateFormatter dateFromString:stringDate];
NSString *newDate = [dateFormatter stringFromDate:date];
But it show null value. What is problem in this code?
Thanks in advance...
It is different from #Mayur Joshi's answer though it looks to be same.
This is sure to work for you.
NSString *dateString = #"2011-09-19";
NSDateFormatter *format = [[NSDateFormatter alloc] init];
[format setDateFormat:#"yyyy-MM-dd"];
NSDate *date = [format dateFromString:dateString];
[format setDateFormat:#"MM-dd-yyyy"];
NSString* finalDateString = [format stringFromDate:date];
[format release];
I hope this helps you.
If you need any help on this then please leave a comment below.
Also you can refer to this link for more help. This link is really similar to what you want, only the target date Format is different.
How to convert date string into format "17 Nov 2010"
NSString *dateStr = #"2011-1-24";
//OR if you have date then declare dateStr like this,
NSString *dateStr = [NSString stringWithFormat:#"%#",yourDate];
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"YYYY-M-dd"];
NSDate *date = [dateFormat dateFromString:dateStr];
[dateFormat setDateFormat:#"MM-dd-YYYY"];
NSString* temp = [dateFormat stringFromDate:date];
[dateFormat release];
NSLog(#"%#",temp);