NSDate and NSString cocoa - iphone

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

Related

Get time from NSDate returns nil

I am trying to show time on graph and i have a full time stamp in this #"2012-08-28 18:50:24" format. when i try to get time from this date then it returns nil in NSDate.
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:#"HH:mm:ss"];
NSDate *time = [formatter dateFromString:#"2012-08-28 18:50:24"];
in this code only if I change [formatter setDateFormat:#"HH:mm:ss"]; line to [formatter setDateFormat:#"yyyy-MM-dd HH:mm:ss"]; then it starts working with full date and time. But i need only time
u have string . so you must have to convert it into date first as per your string format. after that you can convert date into any format. so follow this.
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:#"yyyy-MM-dd HH:mm:ss"];
NSDate *time = [formatter dateFromString:#"2012-08-28 18:50:24"];
[formatter setDateFormat:#"HH:mm:ss"];
NSString *str = [formatter stringfromdate:time];
NSLog(#"%#",str);
NSDateFormatter is used to format your NSDate to your requirements.
Here you are fetching the date from a string so you have to tell the NSDateFormatter to look for the string with that format and convert it into date
NSDate contains both date and time so if you want the time only to show use dateformatter to get the time component from NSDate
iOS manages time and date together in NSDate.Actually they are supposed to work together.
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:#"yyyy-MM-dd HH:mm:ss"];
// GET Proper NSDate
NSDate *time = [formatter dateFromString:#"2012-08-28 18:50:24"];
//To your requirement
[formatter setDateFormat:#"HH:mm:ss"];
NSString *timestr=[formatter stringFromDate:time];
NSLog(#"%#",timestr);
Because, first you need to convert your string into NSDate in the right formate that is
[formatter setDateFormat:#"yyyy-MM-dd HH:mm:ss"];
NSDate *aDate = [formatter dateFromString:#"2012-08-28 18:50:24"];
Now you can reformate the NSDateFormatter according to your need. Now you can get time from the
[formatter setDateFormat:#"HH:mm:ss"];
NSString *newDate = [dateFormatter stringFromDate:aDate];
If you need to produce Date from String then you'll need to set the date format accordingly.
This is important that you create a NSDate object first! Only then you can separate time from whole date.
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:#"yyyy-MM-dd HH:mm:ss"];
NSDate *time = [formatter dateFromString:#"2012-08-28 18:50:24"];
then
[formatter setDateFormat:#"HH:mm:ss"];
NSString *timeStr = [formatter stringFromDate:time];
You can also use other string concatenation functions to remove the Date Modules like this.
However, if you have an NSDate first, then your previous code would work.
NSDate *date = [NSDate date];
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:#"HH:mm:ss"];
NSString *timeStr = [formatter stringFromDate:date];
NSDateFormatter *timeFormat = [[NSDateFormatter alloc] init];
[timeFormat setDateFormat:#"hh:mm:ss a"];
NSString *theTime = [timeFormat stringFromDate:[NSDate date]];
NSLog(#"time, %#",theTime);
First you have converted your string date and time in the date formate..
YourTimeStr = #"2012-08-28 18:50:24";
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
formatter.dateFormat = #"yyyy-mm-dd HH:mm:ss";
NSDate *DateAndTime = [formatter dateFromString:LogOutTimeSTR];
then
NSDateFormatter *dateFormatter1 = [[[NSDateFormatter alloc] init] autorelease];
[dateFormatter1 setDateFormat:#"HH:mm:ss"];
[dateFormatter1 setTimeZone:[NSTimeZone timeZoneWithName:#"UTC"]];
NSString *YourTimeStr =[dateFormatter1 stringFromDate:DateAndTime];

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

How to specify the NSDate Format in Iphone app?

I am trying to make a date format in such a manner hours.minutes day/month/year
I have uses this code :
NSDateFormatter *formatter =[[NSDateFormatter alloc] init];
NSDate *dateAndtime = [NSDate date];
[formatter setTimeStyle:NSDateFormatterShortStyle];
[formatter setDateStyle:NSDateFormatterShortStyle];
//[formatter setTimeStyle:NSDateFormatterShortStyle];
NSString* dateforBD=[[NSString alloc]init];
dateforBD =[formatter stringFromDate:dateAndtime];
But this code not give me my format.
Also i have tried this code:
NSLocale *locale = [NSLocale currentLocale];
NSDateFormatter *formatter = [[[NSDateFormatter alloc] init] autorelease];
NSString *dateFormat = [NSDateFormatter dateFormatFromTemplate:#"E MMM d yyyy"
options:0 locale:locale];
[formatter setDateFormat:dateFormat];
[formatter setLocale:locale];
It's also give me different format. How can i achieve the required format means 13.10 21/4/2011
All the formats return in month before date. May i have used wrong way for this ?
Thanks in advance.
NSDate *today = [NSDate date];
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"MM/dd/yyyy hh:mma"];
NSString *dateString = [dateFormat stringFromDate:today];
NSLog(#"date: %#", dateString);
[dateFormat release];
To explore more visit http://www.stepcase.com/blog/2008/12/02/format-string-for-the-iphone-nsdateformatter/
Hope it helps.
for date before month use -
[dateFormat setDateFormat:#"dd/MM/yyyy hh:mm"];
As specified in Apple's NSDateFormatter reference, the NSDateFormatter uses Unicode Date Format Patterns.
According to the unicode patterns, you would need the following pattern to achieve "13.10 21/4/2011":
hh:mm dd/MM/yyyy
So you would use this code:
NSDate *dateAndtime = [NSDate date];
NSDateFormatter *dateFormatter = [[[NSDateFormatter alloc] init] autorelease];
[dateFormatter setDateFormat:#"hh:mm dd/MM/yyyy"];
NSString *newlyFormattedDateString = [dateFormatter stringFromDate:dateAndtime];

date from string

I have string like #"2011-03-29 15:22:16"
and I use dateformatter to get NSdate obj.
Below is my code:
NSString *dateStr = #"2011-03-29 15:22:16";
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setTimeStyle:NSDateFormatterShortStyle];
[dateFormatter setDateStyle:NSDateFormatterLongStyle];
NSLocale *usLocale = [[NSLocale alloc] initWithLocaleIdentifier:#"en_US"];
[dateFormatter setLocale:usLocale];
NSDate *date = [dateFormatter dateFromString:dateStr];
However, date is always NULL.
I tried several time&date style, and failed either.
So can this kind of string be formatted into the NSDate object?
thanks in advance!
I presume the format is incorrect. Try setting it manually.
NSString *dateStr = #"2011-03-29 15:22:16";
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"yyyy-MM-dd HH:mm:ss"];
NSDate *date = [dateFormatter dateFromString:dateStr];
Maybe you can try following.
mDateFormatter = [[NSDateFormatter alloc] init];
// 2010-11-11 17:26:59
[mDateFormatter setDateFormat:#"yyyy-MM-dd HH:mm:ss"];

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!