String to Date Conversion in iPhone - iphone

I have refer previous examples from here. But still problem in null value.
String Value is :==> strDate = "09-05-2012 00:00:00"
I want output as :==> May 09, 2012
My Code:
NSString *strDateFormat;//[strDate substringWithRange:NSMakeRange(0, 10)];
strDateFormat = [NSString stringWithFormat:#"%#", strDate];
NSDateFormatter *df = [[NSDateFormatter alloc] init];
NSDate *disDate = [df dateFromString:strDateFormat];
[df setDateFormat:#"MMM dd, yyyy"];
strDateFormat = [df stringFromDate:disDate];
lblDate.text = [NSString stringWithFormat:#"%#", strDateFormat];
[df release];
It gives (null) value. Because disDate has (null) value.
Why disDate set to nil..?
Any solution..?

You gotta tell the formatter what format to expect when reading a string, too.
NSString *string = #"09-05-2012 00:00:00";
NSDateFormatter *formatter = [[[NSDateFormatter alloc] init] autorelease];
[formatter setDateFormat:#"yyyy-MM-dd HH:mm:ss"];
NSDate *date = [formatter dateFromString:string];
[formatter setDateFormat:#"MMM dd, yyyy"];
NSString *formattedString = [formatter stringFromDate:date];

-(NSString*)getStringWithFormat:(NSString*)pStrDate fromFormat:(NSString*)pFromFormat toFormat:(NSString*)pToFormat
{
NSDateFormatter *dtFormatter = [[[NSDateFormatter alloc] init] autorelease];
[dtFormatter setDateFormat:pFromFormat];
NSDate *fromFormatDate = [dtFormatter dateFromString:pStrDate];
[dtFormatter setDateFormat:pToFormat];
return [dtFormatter stringFromDate:fromFormatDate];
}
and call it like this
NSString *strDate = [self getStringWithFormat:#"09-05-2012 00:00:00" fromFormat:#"dd-MM-yyyy HH:MM:SS" toFormat:#"MMM dd, yyyy"];
This is common function, You can use it for any formats, Just you have to pass that format in argument.

NSString *strDateFormat; //[strDate substringWithRange:NSMakeRange(0, 10)];
strDateFormat = [NSString stringWithFormat:#"%#", strDate];
NSDateFormatter *df = [[NSDateFormatter alloc] init];
[df setDateFormat:#"MMM dd, yyyy"];
// ###### Set dateformat also here according to strDateFormat
NSDate *disDate = [df dateFromString:strDateFormat];
[df setDateFormat:#"MMM dd, yyyy"];
strDateFormat = [df stringFromDate:disDate];
lblDate.text = [NSString stringWithFormat:#"%#", strDateFormat];
[df release];

you have not set the formater pattern
NSDateFormatter *df = [[NSDateFormatter alloc] init];
[df setFormatterBehavior:NSDateFormatterBehavior10_4];
[df setDateFormat:#"MMM dd, yyyy"];
NSDate *convertedDate = [df dateFromString:origDate];
[df release];

Related

String converson to date with unknown formate iphone

I have a date string like this "2013-09-05T06:40:19Z" from server
i want to convert it into date which format should be same as my iphone follow .
how to do this ?
when i do this ?
NSDateFormatter *dateFormatter1 = [[NSDateFormatter alloc] init];
[dateFormatter1 setDateFormat:#"MMM dd yyyy"];
[dateFormatter1 setLocale:[NSLocale currentLocale]];
[dateFormatter1 setTimeZone:[NSTimeZone systemTimeZone]];
NSDate *date =[dateFormatter1 dateFromString:time];
NSLog(#"%#",date)//getting null here
i am getting null in date.
NSString *dateStr = #"2013-09-05T06:40:19Z";
NSString *outDateFormate = #"MMM dd yyyy";
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setLocale:[NSLocale currentLocale]];
[dateFormat setDateFormat:#"yyyy-MM-dd'T'HH:mm:ss'Z'"];
NSDate *intime = [dateFormat dateFromString:dateStr];
[dateFormat setDateFormat:outDateFormate];
NSString *dateWithNewFormat = [dateFormat stringFromDate:intime];
Since you are getting the date in specified format so you need to define your date formatter accordingly
NSString *gameDateString = #"2013-09-05T06:40:19Z";
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
dateFormatter setDateFormat:#"yyyy-MM-dd'T'HH:mm:ss'Z'"];
[dateFormatter setLocale:[NSLocale currentLocale]];
[dateFormatter setTimeZone:[NSTimeZone systemTimeZone]];
NSDate *gameDate = [dateFormatter dateFromString:gameDateString];
NSLog#("** The date is %#",gameDate);
// Now convert date to string in the format which you required.
[dateFormatter setDateFormat:#"MMMM dd, yyyy"];
NSString *dateString = [dateFormatter stringFromDate:gameDate];
NSLog#("** The date string is %#",dateString);
Hope this helps.
NSString *str=#"2013-09-05T06:40:19Z";
str=[[str stringByReplacingOccurrencesOfString:#"T" withString:#" "]stringByReplacingOccurrencesOfString:#"Z" withString:#""];
NSDateFormatter *dateFormatter1 = [[NSDateFormatter alloc] init];
[dateFormatter1 setDateFormat:#"yyyy-MM-dd hh:mm:ss"];
[dateFormatter1 setLocale:[NSLocale currentLocale]];
[dateFormatter1 setTimeZone:[NSTimeZone systemTimeZone]];
NSDate *date =[dateFormatter1 dateFromString:str];
[dateFormatter1 setDateFormat:#"MMM dd yyyy"];
NSString *newDate=[dateFormatter1 stringFromDate:date];
NSLog(#"%#",newDate)

NString shows NSDate is 000 instead of Actual Date in iphone application

I have application in which i am using NSDate and saving it in NSString but NSString store 000.000 like this here is the code which i am using
NSDate* date = [NSDate date];
NSDateFormatter* formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:#"yyyy-MM-dd HH:MM:SS"];
NSString* str = [formatter stringFromDate:date];
NSString*content_AddedTime=str;
NSString *post =[[NSString alloc] init];
post = [NSString stringWithFormat:#"userId=%#&catalogID=%#&content_AddedTime=%#&organizationCode=%#",userId,catalogID,content_AddedTime,organizationCode];
Try this:
[formatter setDateFormat:#"yyyy/MM/dd HH-mm-ss"];
or you can use:
[formatter setDateFormat:#"EEE, dd MMM yyyy HH:mm:ss ZZZ"];
Hope it will help!
Try to Log the NSString.There is no issue with your code.
NSDate* date = [NSDate date];
NSDateFormatter* formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:#"yyyy-MM-dd HH:MM:SS"];
NSString* str = [formatter stringFromDate:date];
NSString*content_AddedTime=str;
NSLog(#"%#",str);
NSLog(#"%#",content_AddedTime);
OutPut:
2013-07-30 12:07:67
2013-07-30 12:07:67
NSDateFormatter *dateFormatter=[[NSDateFormatter alloc]init];
[dateFormatter setDateFormat:#"MMMM/dd/yyyy"];//set ur format
NSDate *date = [NSDate date];
NSString *str= [dateFormatter stringFromDate:date];

NSDateFormatter how can I format this date?

I am attempting to format an NSString which looks like:
#"2012-07-19T02:58:33Z"
to an NSDate with the following format:
#"MMMM dd, yyyy HH:mm a"
The NSDateFormatter always returns nil and I think it is because it cannot convert the NSString in it's current format to the desired one. Here is my code:
NSDateFormatter *formatter = [[NSDateFormatter alloc]init];
[formatter setDateFormat:#"MMMM dd, yyyy HH:mm a"];
NSLocale *enUSLocale = [[NSLocale alloc] initWithLocaleIdentifier:#"en_US"];
[formatter setLocale:enUSLocale];
NSDate *date = [formatter dateFromString:dateString];
Do I need to store the date in a differant format? Can I make this conversion possible with the current NSString?
Thanks!
Your dateFormatter doesn't match your dateString... Try this
NSDateFormatter *formatter = [[NSDateFormatter alloc]init];
[formatter setDateFormat:#"yyyy-MM-dd'T'HH:mm:ss'Z'"];
NSLocale *enUSLocale = [[NSLocale alloc] initWithLocaleIdentifier:#"en_US"];
[formatter setLocale:enUSLocale];
NSDate *date = [formatter dateFromString:dateString];
[formatter setDateFormat:#"MMMM dd, yyyy HH:mm a"];
dateString=[formatter stringFromDate:date];
now dateString contains "July 19, 2012 02:58 AM"
Hope this helps
Try it.
NSString *myDateString=#"2012-07-19T02:58:33Z";//#"2009-07-06T17:42:12";
NSDateFormatter *dateFormatter = [[[NSDateFormatter alloc] init] autorelease];
[dateFormatter setDateFormat:#"yyyy-MM-dd'T'HH:mm:ss'Z'"];
NSDate *myDate = [[NSDate alloc] init];
myDate = [dateFormatter dateFromString:myDateString];
NSLog(#"MyDate is: %#", myDate);
Your problem has already been answered.
Store your string in a NSDate with the format of the original string:
NSString *originalDateString = #"2012-07-19T02:58:33Z";
NSDateFormatter *originalFormatter = [[NSDateFormatter alloc]init];
[formatter setDateFormat:#"yyyy-MM-dd'T'HH:mm:ss'Z'"];
NSLocale *enUSLocale = [[NSLocale alloc] initWithLocaleIdentifier:#"en_US"];
[formatter setLocale:enUSLocale];
NSDate *originalDate = [formatter dateFromString:originalDateString];
NSDateFormatter *newFormatter = [formatter setDateFormat:#"MMMM dd, yyyy HH:mm a"];
NSString *newDateString = [formatter stringFromDate:originalDate];

NSDateFormatter DateFromString from RSS Feed returning (NULL)

I have allocated a date formatter, and I am trying to format a string which contains a date:
formatter = [[NSDateFormatter alloc] init];
[formatter setDateStyle:NSDateFormatterShortStyle];
[formatter setTimeStyle:NSDateFormatterShortStyle];
NSString *date = [[stories objectAtIndex:storyIndex] objectForKey:#"date"];
NSDate *formattedDate = [formatter dateFromString:date];
NSLog(#"%#", formattedDate);
As you can see I am 'NSLogging' the formattedDate to see if it had worked or not. Unfortunately it has not worked and therefore is returning (NULL).
Please could you tell me what I am doing wrong.
Thanks in advanced.
The string "Fri, 10 Dec 2010 23:48:36 +0000" is not a "short style".
To parse that reliably, set the locale and an explicit date format:
formatter = [[NSDateFormatter alloc] init];
NSLocale *locale = [[[NSLocale alloc]
initWithLocaleIdentifier:#"en_US_POSIX"] autorelease];
[formatter setLocale:locale];
[formatter setDateFormat:#"EEE, dd MMM yyyy HH:mm:ss Z"];
NSString *date = [[stories objectAtIndex:storyIndex] objectForKey:#"date"];
NSDate *formattedDate = [formatter dateFromString:date];
NSLog(#"%#", formattedDate);
Edit:
To then display the NSDate formattedDate in the format "Friday 10 December 2010", add this code after the above:
NSDateFormatter *displayFormatter = [[NSDateFormatter alloc] init];
[displayFormatter setLocale:locale];
[displayFormatter setDateFormat:#"EEEE dd MMMM yyyy"];
NSString *displayDate = [displayFormatter stringFromDate:formattedDate];
NSLog(#"displayDate = %#", displayDate);
[displayFormatter release];
For an explanation of the locale see Apple QA1480.
For an explanation of the date format see Unicode Date Format Patterns.

iPhone SDK Objective-C __DATE__ (compile date) can't be converted to an NSDate

//NSString *compileDate = [NSString stringWithFormat:#"%s", __DATE__];
NSString *compileDate = [NSString stringWithUTF8String:__DATE__];
NSDateFormatter *df = [[[NSDateFormatter alloc] init] autorelease];
[df setDateFormat:#"MMM d yyyy"];
//[df setDateFormat:#"MMM dd yyyy"];
NSDate *aDate = [df dateFromString:compileDate];
Ok, I give up. Why would aDate sometimes return as nil?
Should it matter if I use the commented-out lines... or their matching replacement lines?
It can return nil if the phone's Region setting is not US (or equivalent).
Try setting the formatter's locale to en_US:
NSString *compileDate = [NSString stringWithUTF8String:__DATE__];
NSDateFormatter *df = [[[NSDateFormatter alloc] init] autorelease];
[df setDateFormat:#"MMM d yyyy"];
NSLocale *usLocale = [[NSLocale alloc] initWithLocaleIdentifier:#"en_US"];
[df setLocale:usLocale];
[usLocale release];
NSDate *aDate = [df dateFromString:compileDate];
Slightly modifying DyingCactus' answer for ARC enabled code (for easier copying-n-pasting):
NSString *compileDate = [NSString stringWithUTF8String:__DATE__];
NSDateFormatter *df = [[NSDateFormatter alloc] init];
[df setDateFormat:#"MMM d yyyy"];
NSLocale *usLocale = [[NSLocale alloc] initWithLocaleIdentifier:#"en_US"];
[df setLocale:usLocale];
NSDate *aDate = [df dateFromString:compileDate];