-1

dbVoiceDate is wrong why?

I tried a lot!

NSDateFormatter *formatterVoice = [[NSDateFormatter alloc] init];
formatterVoice.dateFormat = @"dd-MMM-yyyy hh:mm:ss";
dbVoiceDate = [formatterVoice dateFromString:dbDateStr];

Getting response:

Printing description of dbDateStr:

03-Oct-2016 10:43:59

Printing description of self->dbVoiceDate:

2016-10-03 05:02:16 +0000

thanks in advance.

vaibhav
  • 4,038
  • 1
  • 21
  • 51
  • Timezone is the issue, see this http://stackoverflow.com/questions/16621330/nsdateformatter-not-working-to-set-settimezone – Anbu.Karthik Oct 03 '16 at 05:23
  • http://stackoverflow.com/questions/8442706/nsdateformatter-and-time-zone-issue – Anbu.Karthik Oct 03 '16 at 05:23
  • What kind of date you wont tell me .. then i give you perfect answer . – Himanshu Moradiya Oct 03 '16 at 05:54
  • TimeZone issue, no? The small differences on the seconds part is due to something else, and it seems that your date may need `HH` instead of `hh` for the hours, because it doesn't have AM or PM explicitly written.$ – Larme Oct 13 '16 at 13:12
  • There's a gazillion other posts telling you the you need to lear about time zones. – gnasher729 Oct 18 '16 at 09:55

4 Answers4

1

format it like this

NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"dd-MMM-yyyy hh:mm:ss"];
[dateFormatter setTimeZone:[NSTimeZone localTimeZone]];

//then do your coding.
caldera.sac
  • 4,918
  • 7
  • 37
  • 69
1

Try this:-

NSString *dateString = @"Thu Oct 3 10:34:58 +0000 2016";
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"EEE MMM dd hh:mm:ss Z yyyy"];
NSDate *date = [dateFormatter dateFromString:dateString];
NSLog(@"%@", date);
[dateFormatter setDateFormat:@"dd-MM-yyyy hh:mm:ss"];
NSString *finalDateStr = [dateFormatter stringFromDate:date];
NSLog(@"%@", finalDateStr);
User511
  • 1,456
  • 10
  • 28
1

Try this following code:

+(NSString*)getDateWithMonthSpelling:(NSString*)dateStr
{
    NSDateFormatter* dateFormatter = [[NSDateFormatter alloc] init];
    dateFormatter.dateFormat = @"dd/MM/yyyy";
    NSDate *yourDate = [dateFormatter dateFromString:dateStr];
    dateFormatter.dateFormat = @"dd MMM yyyy";
    NSString*myDate = [dateFormatter stringFromDate:yourDate];
    return myDate;
}
Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
G.Anushiya
  • 31
  • 5
0

You need to check the timezone. If it is in GMT set timezone to GMT, like this:

[youDateFormat setTimeZone:[NSTimeZone timeZoneWithName:@"GMT"]];

Else you can use your local timezone like this:

[youDateFormat setTimeZone:[NSTimeZone systemTimeZone]];
halfer
  • 19,824
  • 17
  • 99
  • 186
Rashad
  • 11,057
  • 4
  • 45
  • 73