This question already has answers here:
Converting NSString to NSDate (and back again)
(17 answers)
Closed 9 years ago.
1) I am getting the date from the google calender. The format is 2013-05-03T22:30:00.000+02:00 like this. Which date format used for this type of date in the app.
2) I have to show the date like this format Feb 4th 2013 .
How to implement these two features in the my app? Please help me.
Thank you.
To get the NSDate from that string:
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"yyyy-MM-dd'T'HH:mm:ss.SSS Z"];
NSDate *myDate = [dateFormat dateFromString:#"2013-05-03T22:30:00.000+02:00"];
Once you have your NSDate, you can do the same to get the representation:
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:"MMM d yyyy"];
NSString *myString = [myDate stringFromDate:myDate];
You can consult the whole date formatting options here:
Date Formatter Guide
The date is in RFC3339 format. I suggest you to check this gist where you can see a category on NSDate called NSDate (InternetDateTime). This will do the formatting for your input.
For your second question, check the usage of NSDateFormatter.
Related
This question already has answers here:
What format string do I use for milliseconds in date strings on iPhone?
(4 answers)
Closed 7 years ago.
I need to convert a date string "2015-03-10 00:00:00.000"(string from web service) to NSDate. I have used code like this
NSDateFormatter *fromDateFormat = [[NSDateFormatter alloc] init];
[fromDateFormat setDateFormat:#"yyyy-MM-dd HH:mm:ss"];
NSDate *outputDate = [fromDateFormat dateFromString:#"2015-03-10 00:00:00.000"];
outputDate is nil. I tried without last triple zeros, and it is working. but I can't remove that zeros, because it is from a service.
If you want convert string to date using This function
NSDate *current=[self convertStringToDate:data.strCreateDate formate:#"yyyy-MM-dd HH:mm:ss"];
Put this method in your projects
- (NSDate *)convertStringToDate: (NSString *)date formate:(NSString *)formate {
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:formate];
return [dateFormat dateFromString:date];
}
This is helpfult to you..
This question already has answers here:
Changing date format on iOS
(2 answers)
Closed 8 years ago.
Was wondering if there is an easy conversion to take a date in the format dd/MM/yyyy and transform it to MM/dd/yyyy to display.
Current code tried is:
//in dd/MM/yyyy format string below.
NSString *stringToFormat = #"28/05/1991";
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"MM/dd/yyyy"];
NSDate *date = [dateFormatter dateFromString:stringToFormat];
However, date will return nil in this situation. Any help would be appreciated. Thank you.
You'll have to "decode" and then "encode". You're currently trying to parse 28 as a month. Try this:
//in dd/MM/yyyy format string below.
NSString *stringToFormat = #"28/05/1991";
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"dd/MM/yyyy"];
NSDate *date = [dateFormatter dateFromString:stringToFormat];
[dateFormatter setDateFormat:#"MM/dd/yyyy"];
[dateFormatter stringFromDate:date];
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);
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];
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 %#",)