如何将日期格式化为IST? [英] How to format the date to IST?

查看:198
本文介绍了如何将日期格式化为IST?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须格式化日期如下:



19-JUL-2014 10:27:16 IST



我该怎么做?我应该将IST作为字符串对象发送吗?



我尝试了 -

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


解决方案

我尝试过几种时区格式化程序到苹果官方文档 Unicode date-formatter标准



我把这个时区加了进来:

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

,以 +0530 offset,因此用于我的 NSDateFormatter 的实例。

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

这里是有关我使用不同格式scpecifier的列表:




  • z = GMT + 0530 IST li>
  • zz = GMT + 0530 IST

  • zzz = GMT + 0530 IST

  • zzzz = 印度标准时间IST

  • zzzzz = 印度标准时间IST



似乎没有标准格式说明符可以提供实际的IST仅作为字符串,匹配是印度标准时间IST格式说明符 zzzz zzzzz - 但您可以看到GMT + 0530 IST 仍然包含其他格式化程序。



注意:其他格式说明符如 Z v V x X 似乎也没有用。





我已经阅读了关于格式说明符的更多信息,该文档介绍使用 z


特定非定位格式(例如太平洋夏令时)。哪里不可用,请返回到本地化的格林尼治标准时间格式。


这意味着我,印度标准时间的实际特定非定位格式不能直接通过 NSDateFormatter 获得,或者由于某种原因指定为GMT + 0530 IST不如IST 。 / p>

另一方面,我不知道您的服务器端是否接受长的特定非定位格式(也称为印度标准时间IST),或者时区必须用字符串IST标记。



如果您希望最新的格式只有您需要手动添加,您才需要如下所示:

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






注意:我也发现这几个月的名字也应该被资本化,我不知道是另一个期望或者个人名字的通用大写(例如Jul,Sep等等)对你的服务器端来说足够好 - 我目前的答案没有照顾他们的利益。






†我没有找到任何应​​该描述实际的短格式的标准,所以基于unicode标准,我假设IST应该是缩写的格式GMT + 0530 IST - 但这只是基于我个人的猜测。


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

解决方案

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.

这篇关于如何将日期格式化为IST?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆