9

I have two strings: date1 = 3-3-2011;

and i want to convert in to 3-March-2011 and display it in label.

NSString *myString = 3-3-2011;

NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init];
[dateFormatter setDateFormat:@"d-MM-YYYY"];
NSDate *yourDate = [dateFormatter dateFromString:myString];



//now format this date to whatever you need…
[dateFormatter setDateFormat:@"d-MMM-YYYY"];
NSString *resultString = [dateFormatter stringFromDate:yourDate];
[dateFormatter release];

but yourdate = 2010-12-25 18:30:00 +0000

resultstring = 26-Dec-2010

i want 3-March-2010

please help! Thank you.

Krishnabhadra
  • 34,169
  • 30
  • 118
  • 167
Sam007
  • 1,385
  • 3
  • 17
  • 32

3 Answers3

13

You can use NSDateFormatter.

  1. Convert your current string to NSDate object, so that you can convert it into any format you want.

    NSString *myString          =   @"3-3-2011";
    NSDateFormatter *dateFormatter  =   [[NSDateFormatter alloc]init];
    [dateFormatter setDateFormat:@"d-M-yyy"];
    NSDate *yourDate                =   [dateFormatter dateFromString:myString];
    
  2. Now you can convert this NSDate to any format you want.

    [dateFormatter setDateFormat:@"d-MMMM-yyyy"];
    NSString *resultString          =   [dateFormatter stringFromDate:yourDate];
    [dateFormatter release];
    

Apple's documentation on NSDateFormatter is here.

Krishnabhadra
  • 34,169
  • 30
  • 118
  • 167
  • NSString *myString = 3-3-2011; NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init]; [dateFormatter setDateFormat:@"d-MM-YYYY"]; NSDate *yourDate = [dateFormatter dateFromString:myString]; //now format this date to whatever you need… [dateFormatter setDateFormat:@"d-MMM-YYYY"]; NSString *resultString = [dateFormatter stringFromDate:yourDate]; [dateFormatter release]; but yourdate = 2010-12-25 18:30:00 +0000 resultstring = 26-Dec-2010 i want 3-March-2010 – Sam007 Apr 09 '11 at 12:29
  • 2
    @Sam use `@"d-M-yyyy"`. I have no idea how that answer got 2 upvotes. – Matthias Bauch Apr 09 '11 at 12:36
  • @fluchtpunkt is correct on both counts. The lowercase y is important. See http://unicode.org/reports/tr35/#Date_Format_Patterns. –  Apr 09 '11 at 13:11
  • The format string needs to be `@"d-M-yyyy"` in both cases. @"d-M-LLL" and @"d-MMMM-LLLL" are incorrect. – dawg Apr 09 '11 at 15:17
  • You should set the Style, not the format, if you want to present the dates appropriately. So set the format for the input(from files etc, not user), but use style when displaying. – Bwooce Dec 16 '11 at 00:54
2

Take a look at NSDateFormatter. In particular, take a look at the dateFromString: and stringFromDate: methods. You'll need to convert your original string to an NSDate, then convert that NSDate to another string.

One tip for using NSDateFormatter: be sure always to set a locale. If you don't manually set a locale, it has a bug regarding 12/24 hour clock settings. The example code on the page I linked to shows how to set a locale.

Amy Worrall
  • 16,250
  • 3
  • 42
  • 65
2

Use d-M-Y (or M-d-Y, depending on which is the month):

NSString *myString = 3-3-2011;
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init];
[dateFormatter setDateFormat:@"d-M-Y"];
NSDate *yourDate = [dateFormatter dateFromString:myString];

And see the Unicode standard referred to in the Apple docs.

Hwee-Boon Yar
  • 512
  • 3
  • 13