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

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

Related

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

How to change nsstring to nsdate format in iphone?

I have struggling with to convert nsstring to nsdate format. I have a date in nsstring format like (2011-08-25 18:30:00 +0000), but i want to change it in nsdate format (08/26/2011). How to change it? Can any one help me please? Thanks in advance... I have spend one day to solve this but i cant find out the solution.. Please help me friends....
This is my code,
NSString *dateString = #"2011-08-25 18:30:00 +0000";
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"MM/dd/yyyy"];
NSDate *dateFromString = [[NSDate alloc] init];
dateFromString = [dateFormatter dateFromString:dateString];
NSLog(#"Converted date is %#",dateFromString);
Output is,
Converted date is (null)
Yuva.M
NSString *dateWithInitialFormat = #"2011-08-25 18:30:00 +0000";
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"yyyy-MM-dd HH:mm:ss Z"];
NSDate *date = [dateFormatter dateFromString:dateWithInitialFormat];
[dateFormatter setDateFormat:#"MM/dd/yyyy"];
NSString *dateWithNewFormat = [dateFormatter stringFromDate:date];
NSLog(#"dateWithNewFormat: %#", dateWithNewFormat);
From NSLog: dateWithNewFormat: 08/25/2011
See Date Format Patterns
You need to use an NSDateFormatter and set the date format to match how you're storing it, like the below.
NSDateFormatter *df = [[NSDateFormatter alloc] init];
[df setDateFormat:#"yyyy-MM-dd hh:mm:ss"];
NSDate *myDate = [df dateFromString: myDateString];
NSDateFormatter* df = [[NSDateFormatter alloc] init];
[df setDateFormat:#"MMMM dd, yyyy"];
NSDate* d = [df dateFromString:#"January 06, 1992"];
NSLog(#"%#", d);
NSDate *dispalyDate = d;
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"dd MMM"];
NSString *str = [dateFormat stringFromDate:dispalyDate];
NSLog(#"%#", str);

Manage date with NSDateFormatter

I try to get and manage date with NSDateFormatter but how to make it works.
I think it's a problem with my first "dateFormatter" which have not the right format, because when I use the second formatter with function "stringFromDate" and paramter "[NSDate date]" it works well, but not when I use my NSDate "dateFromString".
//"dateString" have this format: "2011-05-26T16:18:26Z"
NSString *dateString = [[tracks objectAtIndex:0] objectForKey:#"to-date"];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"EEE, MMM d, yyyy"];
NSDate *dateFromString = [[NSDate alloc] init];
dateFromString = [dateFormatter dateFromString:dateString];
[dateFormatter release];
dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"EEE, MMM d, yyyy"];
//NSString *toDate = [dateFormatter stringFromDate:dateFromString];
NSString *toDate = [dateFormatter stringFromDate:[NSDate date]];
Thank you for your help :)
Well of course, the DateFormat is not the same as the one you are receiving from the server.
Try this:
dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"yyyy-MM-dd'T'HH:mm:ss Z"];
NSDate *date = [dateFormatter dateFromString:[dateString stringByReplacingOccurrencesOfString:#"Z" withString:#" +0000"]];
Assuming that to-date is a string and not already an NSDate try this instead:
//"dateString" have this format: "2011-05-26T16:18:26Z"
NSString *dateString = [[tracks objectAtIndex:0] objectForKey:#"to-date"];
NSDate *dateFromString = [[NSDate dateFromString:dateString];
If you want to display a date to the user formatted in a particular format you can then use set up an NSDateFormatter and use stringFromDate: to get a string to show the user. If it still doesn't display correctly set the calendar (I forget if you always have to set it or if there is a default).

NSDate and NSString cocoa

For my iPhone app I got to convert NSDate object to string, and then convert the string back to NSDate object.. Can someone help me out? thank you!
Use to convert from NSDate to NSString
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:#"yyyy-MM-dd hh:mm:ss a"];
NSString *stringFromDate = [formatter stringFromDate:myNSDateInstance];
Use to convert from NSString to NSDate.
NSDateFormatter *df = [[NSDateFormatter alloc] init];
[df setDateFormat:#"yyyy-MM-dd hh:mm:ss a"];
NSDate *myDate = [df dateFromString: stringFromDate];
You need to check out NSDateFormatter this does exactly this, both directions.
convert NSDate to NSString
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:#"yyyy"];
//Optionally for time zone converstions
[formatter setTimeZone:[NSTimeZone timeZoneWithName:#"..."]];
NSString *stringFromDate = [formatter stringFromDate:myNSDateInstance];
Convert NSString to NSDate
NSString *dateString = #"01-02-2010";
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
// this is imporant - we set our input date format to match our input string
// if format doesn't match you'll get nil from your string, so be careful
[dateFormatter setDateFormat:#"dd-MM-yyyy"];
NSDate *dateFromString = [[NSDate alloc] init];
// voila!
dateFromString = [dateFormatter dateFromString:dateString];
[dateFormatter release];

03/20/11 11:24 AM(MONTH/DATE/YEAR HOUR:MIN)

I am using the below code:
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateStyle:NSDateFormatterFullStyle];
NSDate *today=[NSDate date];
NSString *string = [dateFormatter stringFromDate:today];
NSLog(#"%#",string);
[dateFormatter release];
This will not give me this format 03/20/11 11:24 AM(MONTH/DATE/YEAR HOUR:MIN), if i convert system date and time so the converted string should have such type of information 03/20/11 11:24 AM(MONTH/DATE/YEAR HOUR:MIN).? What should i do?
you should set the date format as,
[dateFormat setDateFormat:#"dd/MM/YYYY hh:mm a"];
Try doing this and u will get the format
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:#"MM/dd/yy HH:mm"];
NSDate *today=[NSDate date];
NSString *string = [formatter stringFromDate:today];
NSLog(#"%#",string);
[formatter release];
Good Luck!