NSDateFormatter date format type - ios

I am trying to set the date format for something like "2013-12-02T12:15:43.182Z. I used that code:
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"yyyy'-'MM'-'dd'T'HH':'mm':'ssS''Z'"];
How can i set this date format?

Ues this:
NSDateFormatter * dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setTimeZone:[NSTimeZone systemTimeZone]];
[dateFormatter setDateFormat:#"yyyy-MM-dd'T'HH:mm:ss.SSSZ"];
NSDate *dateConverted = [dateFormatter dateFromString:#"2013-12-02T12:15:43.182Z"];
NSLog(#"%#",dateConverted);

This might work for you..
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"yyyy-MM-dd'T'HH:mm:ss.SSSZ"];

This may help you,
NSDate *myDate = [NSDate date];
NSDateFormatter * df = [[NSDateFormatter alloc] init];
[df setTimeZone:[NSTimeZone systemTimeZone]];
[df setDateFormat:#"yyyy-MM-dd'T'HH:mm:ss.SSSZ"];
NSLog(#"Required date format: %#",[df stringFromDate:myDate]);

Related

Getting nil date in iOS9 when using "[df setDateFormat:#"yyyy-MM-dd HH:mm:ss"];" this format

Here is my code:
NSDate *currentdate = [[NSDate alloc] init];
NSDateFormatter *df = [[NSDateFormatter alloc] init];
[df setDateFormat:#"yyyy-MM-dd HH:mm:ss"];
NSDate *selectedDate = [df dateFromString:SelectedDate];
this gives proper date in iOS8 but in iOS9 giving nil date Please help. thank you.
You can try this. This solved my problem.
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setLocale:[[NSLocale alloc] initWithLocaleIdentifier:#"en_US_POSIX"]];
First of all, you're writing it all wrong. Giving same name to both NSDate and NSString. Try this code, will work.
NSDate *currentdate = [[NSDate alloc] init];
NSDateFormatter *df = [[NSDateFormatter alloc] init];
[df setDateFormat:#"yyyy-MM-dd HH:mm:ss"];
NSString *selectedDate = [df stringFromDate:currentdate];
NSDate *newDate = [df dateFromString:selectedDate];
newDate and currentdate will be same though.
If your SelectedDate does not conform exactly to the date format in your date formatter, the result will be nil. It appears that is your problem.

How to convert datetime string into NSDate in iOS?

I have datetime format like this
2015-10-06 12:39:12
when i try to convert into nsdate it return me null. i am using below code for it.
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:#"yyyy-MM-dd HH:mm:ss Z"];
NSDate *date = [formatter dateFromString:user_post.modification_date];
Please tell me how can i convert it into correct NSDate?
dateString is your date in string format.
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setTimeZone:[NSTimeZone timeZoneWithName:#"GMT"]];
[dateFormatter setDateFormat:#"yyyy-MM-dd HH:mm:ss"];
NSDate *dateFromString = [[NSDate alloc] init];
dateFromString = [dateFormatter dateFromString:dateString];
Try to set like this
NSDate *currDate = [NSDate date];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init];
[dateFormatter setDateFormat:#"YYYY-MM-dd HH:mm:ss"];
NSString *dateString = [dateFormatter stringFromDate:currDate];
Change this line only.
[formatter setDateFormat:#"yyyy-MM-dd HH:mm:ss"];
Z is the timeZone
I suggest you to set the timezone, can be the cause of the problem
[formatter setTimeZone:[NSTimeZone timeZoneWithName:#"GMT"]];
[formatter setLocale:[[NSLocale alloc] initWithLocaleIdentifier:#"en_US"]];

how can get date from string ? my code is enclosed here by

NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setLocale:[[NSLocale alloc] initWithLocaleIdentifier:#"en_US_POSIX"]];
NSDate *date1 =[[NSDate alloc] init];
date1 = [dateFormat dateFromString:#"11-nov-2013"];
NSLog(#"date is %#",date1); // it returns null
self.datePicker.date = date1;
[dateFormat release];
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:#"dd-MMM-yyyy"];
NSDate *myDate = [formatter dateFromString:#"11-nov-2013"];
NSLog(#"date is %#",myDate);
Set date format to
[dateFormat setDateFormat:#"dd-MM-yyyy"];
You need to set the date format as well :
[dateFormat setDateFormat:#"dd-MM-yyyy"];

Date Format - time zone

I receive from the server a string with a date that looks like this: "2013-03-29T15:27:00Z". How can I format it and transform it into an NSDate?
Try this:
[NSDateFormatter setDefaultFormatterBehavior:NSDateFormatterBehavior10_4];
NSDateFormatter *formatter = [[[NSDateFormatter alloc] init] autorelease];
[formatter setDateFormat:#"yyyy-MM-dd'T'HH:mm:ssZ"];
NSDate *date = [formatter dateFromString:#"2013-03-29T15:27:00Z"];
NSString *departTimeDate = #"2013-03-29T15:27:00Z";
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"yyyy'-'MM'-'dd'T'HH':'mm':'ss'Z'"];
NSDate *date = [dateFormatter dateFromString:departTimeDate];
NSLog(#"Expected Result___ %#",date);
NSString *string=#"2013-03-29T15:27:00Z";
NSDateFormatter *dateFormatter=[NSDateFormatter new];
[dateFormatter setDateFormat:#"yyyy-MM-dd'T'HH:mm:ss'Z'"];
NSDate *date=[dateFormatter dateFromString:string];

convert the time to a specific format

I have the current time format returned from the web service:
2012-06-25T23:45:52.664Z
I want to get something like:
2012 june 25 23:45
So without seconds and the Z format indicator.
My relevant code is:
myConvertedTime = [[myActualTime dateFromString] stringFromDate];
try these
NSDate *dateTemp = [[NSDate alloc] init];
NSDateFormatter *dateFormat1 = [[NSDateFormatter alloc] init];
[dateFormat1 setDateFormat:#"yyyy MMM dd hh:mm"];
dateTemp = [dateFormat1 dateFromString:newInvoice.date];
You should use an NSDateFormatter object. And rather than specify a format yourself, it's best to let the framework pick the one that suits based on the user's internationalization settings. This is done as follows:
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateStyle:NSDateFormatterLongStyle];
[formatter setTimeStyle:NSDateFormatterShortStyle];
myConvertedTime = [formatter stringForObjectValue:[myActualTime dateFromString]];
[formatter release];
try this:
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:#"yyyy MMMM dd HH:mm"];
NSString *myConvertedTime = [formatter stringFromDate:now];
Try the following code :
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"yyyy'-'MM'-'dd'T'HH':'mm':'ss"];
NSString *dateString = #"2012-06-25T23:45:52.664Z";
dateString = [dateString substringToIndex:[dateString length]-5];
NSDate *date = [dateFormatter dateFromString:dateString];
[dateFormatter release];
NSDateFormatter *dateFormatter2 = [[NSDateFormatter alloc] init];
[dateFormatter2 setDateFormat:#"yyyy MMMM dd HH:mm"];
NSString *dateText = [dateFormatter2 stringFromDate:date];
[dateFormatter2 release];
NSLog(#"the date is:%#",dateText);

Resources