3

I have to format the date as below:

19-JUL-2014 10:27:16 IST

How can I do that? Should I send "IST" as string object?

I tried -

NSDate* sourceDate = [NSDate date];  
NSLog(@"Date is : %@", sourceDate);  

NSTimeZone *currentTimeZone = [NSTimeZone localTimeZone];  
NSLog(@"TimeZone is : %@", currentTimeZone);  

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

NSLog(@"%@",[dateFormatter stringFromDate:sourceDate]);  
Ryan
  • 4,799
  • 1
  • 29
  • 56
Smitha
  • 6,110
  • 24
  • 90
  • 161
  • Check this same kind of issue. [1]: http://stackoverflow.com/questions/9084980/nsdateformatter-doesnt-show-time-zone-abbreviation-for-asia-kolkata-for-the – vishnuvarthan Jul 24 '14 at 08:42
  • http://eureka.ykyuen.info/2010/06/17/iphone-display-date-and-time-using-nsdateformatter/ – Iphonenew Jul 24 '14 at 08:52
  • I checked stackoverflow.com/questions/9084980/… – @vishnuvarthan - But I received - inccu instead of IST for this! – Smitha Jul 24 '14 at 08:56
  • timezone `IST` is equal to timezone `+0530`, so you can use that instead, like `"19-JUL-2014 10:27:16 +0530"`. – holex Jul 24 '14 at 09:03
  • no i have to send IST in NSDictionary for a service request to hit the web service – Smitha Jul 24 '14 at 09:09

3 Answers3

3

I've tried several of scenarios of timezone formatters according to the Apple's official docs and the Unicode date-formatter standards.

I inited the timezone like this:

NSTimeZone *_timezone = [NSTimeZone timeZoneWithName:@"IST"];

that presented the proper timezone to me with +0530 offset, so that was used for the instance of my NSDateFormatter.

NSDateFormatter *_dateFormatter = [[NSDateFormatter alloc]init];
[_dateFormatter setTimeZone:_timezone];

here is the list about I've experienced with different format-scpecifiers:

  • z = GMT+0530 IST
  • zz = GMT+0530 IST
  • zzz = GMT+0530 IST
  • zzzz = India Standard Time IST
  • zzzzz = India Standard Time IST

it seemed that none of the standard format-specifiers could provide the actual "IST" only as string, a match was the "India Standard Time IST" with format specifier zzzz and zzzzz – but you can see the "GMT+0530 IST" still contains it with the rest of the formatters.

NOTE: the other format specifiers like Z, v, V, x or X did not seem useful either.


I've read more about format specifiers, the docs says about using z:

The short specific non-location format (e.g. PDT). Where that is unavailable, falls back to the short localized GMT format.

that means to me, the actual short specific non-location format for India Standard Time is not available via NSDateFormatter directly – or for some reason is specified as "GMT+0530 IST" not as short "IST".

on the other hand, I'm not sure whether the long specific non-location format is accepted on your server side (aka "India Standard Time IST"), or the timezone must be marked by string "IST" only.

I'm afraid if that latest format is expected only you will need to add it manually and inelegantly, like:

[_dateFormatter setDateFormat:@"dd-MMM-yyyy HH:mm:ss"];
NSString *_date = [[_dateFormatter stringFromDate:[NSDate date]] stringByAppendingString:@" IST"];

NOTE: I've also spotted the months' names should be capitalised as well, I'm not sure that is another expectation or the generic capitalisation of months' names (like e.g. "Jul", "Sep" etc...) is good enough for your server side – I did not take care of capitalising them in my current answer.


† I have not found any standard which to be supposed to describe the actual short format, so based on the unicode standards I would assume the "IST" should be the shortened format against the "GMT+0530 IST" – but that is based on my personal speculation only.

holex
  • 23,961
  • 7
  • 62
  • 76
1
  NSDate* sourceDate = [NSDate date];
    NSLog(@"Date is : %@", sourceDate);

    NSTimeZone *currentTimeZone = [NSTimeZone localTimeZone];
    NSLog(@"TimeZone is : %@", currentTimeZone);

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

    NSLog(@"%@",[dateFormatter stringFromDate:sourceDate]);

This may help you

Iphonenew
  • 299
  • 2
  • 11
0

Please try this,

NSDate *date= [NSDate date];
NSDateFormatter *dateFormatter1 = [[NSDateFormatter alloc] init];
[dateFormatter1 setDateFormat:@"dd-MMM-yyyy HH:mm:ss z"];
NSLog(@"%@",[dateFormatter1 stringFromDate:date]);
Akhil Shrivastav
  • 427
  • 3
  • 13