1

I want to create a NSDate variable from a string, that date is coming from third party server so i cannot change it. the date is in this format December 08, 2013 and i am using given below date formater style to convert it into date but that is creating a wrong and fixed date for all items which is 2012-12-22 19:00:00 +0000

Will you please guide me how to solve this problem. i donot want break string and use NSDateComponents to create date.

[dateFormaterForSermonTemp setDateFormat:@"MMMM dd, YYYY"];
Mani
  • 17,549
  • 13
  • 79
  • 100
Iqbal Khan
  • 4,587
  • 8
  • 44
  • 83
  • 4
    Don't use uppercase YYYY. Use lowercase yyyy. –  Dec 11 '13 at 13:00
  • 1
    And note that the resulting NSDate object will *always* display in the format you show above, and in GMT -- you cannot change that format. If you want to display in a different format run the date back through an NSDateFormatter to produce a string. (In fact, you should never use the format produced by `@"%@"` on an NSDate, as that is for diagnostics only and can, in theory, change at any time.) – Hot Licks Dec 11 '13 at 13:33
  • @Anna what is difference between YYYY and yyyy – Iqbal Khan Dec 12 '13 at 05:29
  • @Developer, see http://stackoverflow.com/questions/4467173/nsdateformatter-dateformatfromtemplateoptionslocale-bug and http://stackoverflow.com/questions/15133549/difference-between-yyyy-and-yyyy-in-nsdateformatter. –  Dec 12 '13 at 11:50

4 Answers4

5

You can use the following code :

NSString *stringDate = @"December 08, 2013";

// Convert string to date object
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:@"MM dd, yyyy"];
NSDate *date = [dateFormat dateFromString:stringDate];

// Convert date object to desired output format
[dateFormat setDateFormat:@"MMMM dd, yyyy"];
stringDate = [dateFormat stringFromDate:date];

NSLog(@"%@", stringDate);

stringDate is the string from your server, you convert it to a NSDate and you add a new format for the date like @"MMMM dd, yyyy".

(Here, you will have the same result but change the format like @"MMMM-dd-yyyy EEE" for example to get another format date).

EDIT :

Maybe you need to set the timeZome for your date

[dateFormat setTimeZone:[NSTimeZone timeZoneWithAbbreviation:@"GMT+0:00"]];
Jordan Montel
  • 8,227
  • 2
  • 35
  • 40
0
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"MM dd, yyyy"];
NSDate *date = [dateFormat dateFromString:@"December 08, 2013"];
Suhit Patil
  • 11,748
  • 3
  • 50
  • 60
0

Here you have documentation about date format string. (the rod not the fish).

So your format string for date December 08, 2013 should be something like (the fish):

@"MMMM dd, y"

and don't forget to set proper locale for the date formater or you can have unexpected problems on devices with different locale settings.

Marek R
  • 32,568
  • 6
  • 55
  • 140
  • The critical thing is the lower-case "y". Upper-case "Y" has an oddly different meaning that mucks up everything. – Hot Licks Dec 11 '13 at 13:35
  • see my link, documentation is blurry how capital `Y` works in format string. It says it is used in "year-week calendar" so week information probably is required to make capital `Y` work properly. – Marek R Dec 11 '13 at 13:53
0
NSString *strDate=@"December 08, 2013";

NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setTimeStyle:NSDateFormatterNoStyle];
[dateFormatter setDateStyle:NSDateFormatterMediumStyle];

NSDate *date=[dateFormatter dateFromString:strDate];

Try this...

Sandeep Ahuja
  • 931
  • 6
  • 17