-3

I'm trying to get the date "30/06/2013" from the button:

I tried this code but I got in the log: 2013-01-04 22:00:00 +0000:

NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@"dd/MM/YYYY"];

NSDate *date = [formatter dateFromString:dateBtn.titleLabel.text];
NSLog(@"date %@",date);

and in the app i got this date "05/01/2013":

I also tried this code but it didn't work:

NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
NSLocale *locale = [[NSLocale alloc] initWithLocaleIdentifier:@"he_IL"];
[dateFormat setLocale:locale];
[dateFormat setDateFormat:@"dd/MM/YYYY"];
NSTimeInterval interval = 5 * 60 * 60;
NSDate *date1 = [dateFormat dateFromString:dateBtn.titleLabel.text];
date1 = [date1 dateByAddingTimeInterval:interval];

It is important to note I use locale Hebrew.

Marcelo
  • 9,916
  • 3
  • 43
  • 52

2 Answers2

2

Have a look at Apple's official documentation on date formatters. Please note how the format differs slightly across different platforms and versions.

Also note the difference between yyyy and YYYY.

fzwo
  • 9,842
  • 3
  • 37
  • 57
2

set date format to "dd/MM/yyyy"

    NSString *originalDate = @"30/06/2013";
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateFormat:@"dd/MM/yyyy"];

    NSDate *date = [formatter dateFromString:originalDate];
    NSLog(@"date %@",date);

The explanation from @fzwo apple link

It uses yyyy to specify the year component. A common mistake is to use YYYY. yyyy specifies the calendar year whereas YYYY specifies the year (of “Week of Year”), used in the ISO year-week calendar. In most cases, yyyy and YYYY yield the same number, however they may be different. Typically you should use the calendar year.

Yan
  • 3,533
  • 4
  • 24
  • 45