How to convert string data into date format in ios? - iphone

In my app, i am taking date and time in to string " 2013-06-11 10:15:00 +0000 " like this, but how can i convert this string into date format. I tried like below code but i am getting error in converting.
NSString *string=#"03-05-2014";
NSDateFormatter *dateFormatter = [[[NSDateFormatter alloc] init] autorelease];
[dateFormatter setDateFormat:#"dd-MM-yyyy"];
NSDate *dateFromString = [dateFormatter dateFromString:string];
Would you please help me in this problem?

Try to do this:
NSDateFormatter* f = [[[NSDateFormatter alloc] init] autorelease];
[f setDateFormat:#"yyyy-MM-dd hh:mm:ss"];
NSDate *dateFromString = [f dateFromString:string];
You have to set the same date format in which the (string or date)contains, otherwise the date is returned by NSDateFormatter will be nil.

You can Use NSCalender Like this.....
NSCalendar *currentCalendar = [NSCalendar currentCalendar];
Refer This Link : Here

NSString *urDateString=#"2013-06-11 10:15:00 +0000";
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"yyyy-MM-dd HH:mm:ss"];
NSString *strDate = [urDateString stringByReplacingOccurrencesOfString:#"+0000" withString:#""];
NSDate *dateString = [dateFormatter dateFromString:strDate];
[dateFormatter setDateFormat:#"MM-DD-yyyy"];
NSString *formattedDateString = [dateFormatter stringFromDate:dateString];
NSLog(#"%#",formattedDateString);

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

How can I convert string into date?

I have two strings:
date1 = 3-5-2014;
date2 = 4-2-2010;
I have to convert them in date and then compare them. I want the same format of date as in strings i.e., dd-mm-yyyy. How it can be done?
NSString *string=#"03-05-2014";
NSDateFormatter *dateFormatter = [[[NSDateFormatter alloc] init] autorelease];
[dateFormatter setDateFormat:#"dd-MM-yyyy"];
NSDate *dateFromString = [dateFormatter dateFromString:string];
enjoy...
You can extract NSDate objects from strings using the NSDateFormatter class. See the Date Formatters section Apple's Data Formatting Guide for a detailed explanation and sample code.
Try with below
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"MM/dd/yyyy h:mm a"];
NSDate *date = [dateFormat dateFromString:dateStr];
[dateFormat release];
here is the SO post
Convert NSString date to NSDate
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"dd-MM-yyyy"];
NSDate *date = [dateFormatter dateFromString:#"25-8-2010"];
NSLog(#"%#", date);
[dateFormatter release];
Hope this works for you
Cheers :)