Date Formatter issue in ObjC [duplicate] - ios

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..

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

NSDateFormat dd/MM/yyyy to MM/dd/yyyy iOS [duplicate]

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

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

Date formatter for the google calender API [duplicate]

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.

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