Formatting a Date String using NSDateFormatter - iphone

I have a NSString which is passed from an xml feed........
NSString *strDate =#"May 14 2010";
I'm currently using this code to format the date........
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateStyle:NSDateFormatterMediumStyle];
[dateFormatter setTimeStyle:NSDateFormatterShortStyle];
[dateFormatter setDateFormat:#"d:MMM"];
NSDate *myDate = [dateFormatter dateFromString:strDate];
NSLog(#"%#", [dateFormatter stringFromDate:myDate]);
I want to format my string only to display day and month and currently I'm getting a nil value to the NSDate. Can anyone please help me with this?......
Thanks.

I think you're slightly misunderstanding how NSDateFormatter works. It cannot automatically work out what format the date in your string is in.
If you change your code to something like this:
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"MMM dd yyyy"];
NSDate *myDate = [dateFormatter dateFromString:strDate];
[dateFormatter setDateFormat:#"d:MMM"];
NSLog(#"%#", [dateFormatter stringFromDate:myDate]);
It should do what you want.

Related

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

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

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