convert NSString to NSDate [duplicate] - iphone

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

Related

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 convert NSString in to NSDate format [duplicate]

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

How to parse an ISO8601 date [duplicate]

This question already has answers here:
How to parse a date string into an NSDate object in iOS?
(7 answers)
Closed 9 years ago.
I got string of date regarding this format:
2011-12-29T09:09:06-0500
How can I convert this into a date object?
Try this solution
NSString *dateString = #"2011-12-29T09:09:06-0500";
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"yyyy-MM-dd'T'HH:mm:ssZ"];
NSDate *dateFromString = [dateFormatter dateFromString:dateString];
[dateFormatter release];
NSLog(#" dateFromString %#",dateFromString);
NSString *date = [[NSDate date] description];
[NSDateFormatter setDefaultFormatterBehavior:NSDateFormatterBehavior10_4];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"yyyy'-'MM'-'dd'T'HH':'mm':'ss'Z'"];
NSDate *dateFromString = [[NSDate alloc] init];
dateFromString = [dateFormatter dateFromString:<NSString containing date>];

Accurate Date formate for hours time and date month in iphone? [duplicate]

I am new in iphone, i want to change string into accurate date-formate which i couldn't done this for last 2 hrs. I have string like(17:30 18 Oct) and want to convert this in accurate formate and also compare this date to tomorrow if this in tomorrow then display just 18 Oct and if it on today just display time 17:30, any help please.
NSString to NSDate conversion
NSString *dateString = #"6:30 18 Oct";
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"hh:mm dd/MMM"];
NSDate *dateFromString = [[NSDate alloc] init];
dateFromString = [dateFormatter dateFromString:dateString];
[dateFormatter release];
Try this
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"hh:mma dd/MMM"];
NSString *dateString = [dateFormat stringFromDate:today];
NSLog(#"date: %#", dateString);
[dateFormat release];

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