How to convert NSString in to NSDate format [duplicate] - iphone

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Converting NSString to NSDate (and back again)
How to convert this NSString(Tue, 13 Mar 2012 00:00:00 -0500) into NSDate format

NSDateFormatter uses the Unicode Date Format Patterns.
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init];
[dateFormat setDateFormat:#"EEE, dd MMM yyyy HH:mm:ss ZZZ"];
NSDate *date = [dateFormatter dateFromString:stringDate ];
[dateFormatter release];

Try this
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"eee, dd MMM yyyy HH:mm:ss ZZZ"];
NSDate *dateFromString = [[NSDate alloc] init] ;
dateFromString = [dateFormatter dateFromString:str];
[dateFormatter release];

Related

convert date string to NSDate in objective c/iOS

I have this string ----> Sun, 16 Dec 2012 15:30:22 +0000
I want to convert it to NSDate. I have tried the below code but it is giving me null
NSDateFormatter *df = [[NSDateFormatter alloc] init];
[df setDateFormat:#"yyyy-MM-dd hh:mm:ss a"];
NSDate *date = [[NSDate alloc] init];
date = [df dateFromString: #"Sun, 16 Dec 2012 15:30:22 +0000"];
NSLog(#"date: %#", date);
I am not getting the issue ?
Any guidance plz
The dateFormat of the formatter needs to match the format of the string you are providing it with.
Therefore something like this should work
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
formatter.dateFormat = #"EEE, dd MMM yyyy HH:mm:ss ZZZ";
NSLog(#"%#", [formatter dateFromString:#"Fri, 18 Jan 2013 15:30:22 +0000"]);
To look up what the specifiers actually mean check Unicode Technical Standard #35
Try this code
NSDateFormatter *df = [[NSDateFormatter alloc] init];
[df setDateFormat:#"EEEE, dd MMM yyyy"];
NSDate *date = [[NSDate alloc] init];
date = [df dateFromString: #"Sun, 16 Dec 2012"];
NSLog(#"date: %#", date);
Hope it helps you..
EDIT :-
For your string
NSDateFormatter *df = [[NSDateFormatter alloc] init];
[df setDateFormat:#"EEEE, dd MMM yyyy HH:mm:ss z"];
NSDate *date = [[NSDate alloc] init];
date = [df dateFromString: #"Sun, 16 Dec 2012 15:30:22 +0000"];
NSLog(#"date: %#", date);
Try this to get NSDate from String
- (NSDate*) dateFromString:(NSString*)aStr
{
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setLocale:[[[NSLocale alloc] initWithLocaleIdentifier:#"en_US_POSIX"] autorelease]];
//[dateFormatter setDateFormat:#"YYYY-MM-dd HH:mm:ss a"];
[dateFormatter setDateFormat:#"EEE, dd MMM yyyy HH:mm:ss ZZZ"];
[dateFormatter setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]];
NSLog(#"%#", aStr);
NSDate *aDate = [dateFormatter dateFromString:aStr];
[dateFormatter release];
return aDate;
}
It works fine for me.

NSDateFormatter is returning null date

Below is my code
NSString *dateandtime =[NSString stringWithFormat:#"%#",self.eventtime];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"EEE, dd MMM yyyy HH:mm:ss Z"];
NSDate *dateFromString = [[NSDate alloc] init];
dateFromString = [dateFormatter dateFromString:dateandtime];
if i print my self.event name i am getting the output as Mon, 7 Jan 2013 15:30:00 CST but when i pass this string in NSDateFormatter i am getting null value i don't know why i tried many combinations of date formats, but i cant solve it, can anyone help me
i solved my problem as Per Prateek comments, now its working for me...Thanks Prateek.
in your self.event, you are getting CST, it should be time zone for example +0530
Use :
[dateFormatter setDateFormat:#"EEE, dd MMM yyyy HH:mm:ss z"];
Use this code...
- (NSString *)formattedDate:(NSString*)dateSte {
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[NSTimeZone resetSystemTimeZone];
NSTimeZone *gmtZone = [NSTimeZone systemTimeZone];
[dateFormatter setTimeZone:gmtZone];
[dateFormatter setDateFormat:#"yyyy-MM-dd"];
NSDate *date = [dateFormatter dateFromString:dateSte];
[dateFormatter setDateFormat:#"EEE,MMM d,''yyyy"];
NSString *strDate = [dateFormatter stringFromDate:date];
return strDate;
}
Use the below method for getting the NSDate from NSString date
NSDate *dateFromString = [[NSDate alloc] init];
dateFromString = [self convertStringToDate:dateandtime];
Paste this below method in your .m file...
-(NSDate *)convertStringToDate:(NSString *) date {
NSDateFormatter *formatter = [[[NSDateFormatter alloc] init] autorelease];
NSDate *nowDate = [[[NSDate alloc] init] autorelease];
[formatter setDateFormat:#"EEE, d MMM yyyy HH:mm:ss 'CST'"];// set format here format in which the date should be
date = [date stringByReplacingOccurrencesOfString:#"+0000" withString:#""];
nowDate = [formatter dateFromString:date];
// NSLog(#"date============================>>>>>>>>>>>>>>> : %#", nowDate);
return nowDate;
}
For More Information About NSDate component see this post of My Blog NSDate-formate-info-for-iphone-sdk

How to Change Date and Time Format? [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How to change the date format in iphone sdk
Actually I'm getting a string in 24 Hours time format. I want to display 12 hours time format.
Here's my input :
2012-10-19T04:10:00
2012-10-06T14:00:00
I would like to have the following format :
10-19-2012 4:10 AM
10-06-2012 2:00 PM
:use the NSDateFormatter to change the date
Here Your input:
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"yyyy-MM-dd'T'HH:mm:ss"];
NSDate *Yourcurrenttime = [dateFormatter dateFromString:Yourstring];
And date converted to the String ..
NSDateFormatter *dateFormatterNew = [[NSDateFormatter alloc] init];
[dateFormatterNew setDateFormat:#"mm-dd-yyyy hh:mm a"];
NSString *stringForNewDate = [dateFormatterNew stringFromDate:Yourcurrenttime];
Use NSDateFormatter with format type dd-MM-yyyy hh:mm:ss a.
Hope this helps you..
NSDate *currentDate = [NSDate date];
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:#"dd-MM-yyyy hh:mm a"]; //You can set different formats for the date
NSLog(#"Current Date %#",[formatter getStringFromDate:currentDate"]);
[formatter release];
In most cases, you’d just need to use the default styles defined in NSDateFormatterStyle.
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateStyle:NSDateFormatterMediumStyle];
[dateFormatter setTimeStyle:NSDateFormatterShortStyle];
NSLog(#"%#", [dateFormatter stringFromDate:[NSDate date]]);
[dateFormatter release];
// Output: Dec 2, 2008 3:58 PM
also you can get output from coding with DateFormatter like bellow..
NSDateFormatter *dateFormatter = [[[NSDateFormatter alloc] init] autorelease];
[dateFormatter setDateFormat:#"MM-dd-yyy HH:mm:s a"];
NSString *convertedString = [dateFormatter stringFromDate:[NSDate date]];
NSLog(#"Converted String : %#",convertedString);
:)

NSDate from string

I have a string "2012-09-16 23:59:59 JST"
I want to convert this date string into NSDate.
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"yyyy-MM-dd hh:mm:ss Z"];
NSDate *capturedStartDate = [dateFormatter dateFromString: #"2012-09-16 23:59:59 JST"];
NSLog(#"%#", capturedStartDate);
But it is not working. Its giving null value. Please help..
When using 24 hour time, the hours specifier needs to be a capital H like this:
[dateFormatter setDateFormat:#"yyyy-MM-dd HH:mm:ss Z"];
Check here for the correct specifiers : http://unicode.org/reports/tr35/tr35-6.html#Date_Format_Patterns
However, you need to set the locale for the date formatter:
// Set the locale as needed in the formatter (this example uses Japanese)
[dateFormat setLocale:[[NSLocale alloc] initWithLocaleIdentifier:#"ja_JP"]];
Full working code:
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"yyyy-MM-dd HH:mm:ss zzz"];
[dateFormatter setLocale:[[NSLocale alloc] initWithLocaleIdentifier:#"ja_JP"]];
NSDate *capturedStartDate = [dateFormatter dateFromString: #"2012-09-16 23:59:59 JST"];
NSLog(#"Captured Date %#", [capturedStartDate description]);
Outputs (In GMT):
Captured Date 2012-09-16 14:59:59 +0000
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"yyyy-MM-dd HH:mm:ss Z"];
NSDate *capturedStartDate = [dateFormatter dateFromString: #"2012-09-16 23:59:59 GMT-08:00"];
NSLog(#"%#", capturedStartDate);
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];
// end
dateFromString = [dateFormatter dateFromString:dateString];
[dateFormatter release];

convert NSString to NSDate [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
NSString to NSDate
Hi I want to convert the string "OCT 1, 2011 18:30" into NSDate format 2011-05-13 19:00:00 +0800.
I have tried with the many formatters but what I am getting is a null value each time.
Can any body help me on this?
You can try this code that will help you because I run successfully.
NSString *dateStr = #"OCT 1, 2011 18:30";
NSDateFormatter *dtF = [[NSDateFormatter alloc] init];
[dtF setDateFormat:#"LLL d, yyyy HH:mm"];
NSDate *d = [dtF dateFromString:dateStr];
NSDateFormatter *dateFormatStr = [[NSDateFormatter alloc] init];
[dateFormatStr setDateFormat:#"yyyy-MM-dd HH:mm:ss Z"];
NSDate *st = (NSDate *)[dateFormatStr stringFromDate:d];
NSLog(#"%#",st);
try this
NSDateFormatter *datefor = [[[NSDateFormatter alloc] init] autorelease];
[datefor setDateFormat:#"MMM dd, yyyy HH:mm"];
NSDate *date = [datefor dateFromString:#"OCT 1, 2011 18:30"];
[datefor setDateFormat:#"yyyy-MM-dd hh:mm:ss"];
NSString *str= [[NSString alloc] initWithString:[datefor stringFromDate:date]];
NSDate *date_fianl = [datefor dateFromString:str];
[str release];
NSLog(#"%#",date_fianl);//it will print 2011-10-01 01:00:00 +0000