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 -
N
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
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.
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]);