2013-05-10 88 views
3

据我所知,日期格式会根据语言环境而有所不同 有没有一种方法可以从当前日期的设备本地设置中读取日期格式?基于语言环境的设备的日期格式

  1. 是否日期格式为“dd/MM/yyyy”或“MM/dd/yyyy”。
  2. 时间格式为“h:mm a”或其他。
  3. AM/PM文本是“AM”或“PM”。

我无法承受任何第三方库,请告诉我这是否可以使用任何苹果类来完成。

编辑:

我不想设置任何格式,我只是想从基于现场设备进行检索。

说,在我的国家这里的日期格式是dd/MM/yyyy这个词的某个部分,它的MM/dd/yyyy我希望得到这种格式。

任何答案表示赞赏。

+7

使用NSDateFormatter设置样式,它应该使用用户的区域设置格式。祝你好运。 – Krishanbhag 2013-05-10 11:35:33

回答

8

我想,这只是一个简单的修复。但在看到这个问题的一些很酷的答案后,对此很好奇。这是我的解决方案。

您可以使用NSDateFormatter这一点,样式设置为需要的格式

NSDateFormatterShortStyle 
NSDateFormatterMediumStyle 
NSDateFormatterLongStyle 

了解更多请看看documentation

下面是你的片段。

NSDateFormatter *dateFormatter = [[[NSDateFormatter alloc] init] autorelease]; 
    [dateFormatter setDateStyle:NSDateFormatterShortStyle]; 
    NSString *dateformat = [dateFormatter dateFormat]; //M/d/yy 
    NSString *amtext = [dateFormatter AMSymbol];  //AM 
    NSString *pmtext = [dateFormatter PMSymbol];  //PM 

    dateFormatter = [[[NSDateFormatter alloc] init] autorelease]; 
    [dateFormatter setTimeStyle:NSDateFormatterShortStyle]; 
    NSString *timeformat = [dateFormatter dateFormat]; //h:mm a 

它将使用用户的区域设置格式。希望解决您的问题。

+1

这是我正在寻找的那种答案。感谢马恩.. !!! – Jacob 2013-05-13 12:22:11

0

您可以使用NSDateFormatter的dateStyle和timeStyle来实现此目的。

NSDateFormatter *dateFormatter = [NSDateFormatter new]; 

//Only set what you want to show, if you set both, both will be included in the converted string 

//This would be like dd/MM/yyyy depending on your locale 
[dateFormatter setDateStyle:NSDateFormatterShortStyle]; 
//This would be in hh:mm a 
[dateFormatter setTimeStyle:NSDateFormatterShortStyle]; 

//Assuming you have a date instance already in hand 
NSString *dateString = [dateFormatter stringFromDate:date]; 
+0

@Downvoter这将是很高兴知道这个答案有什么问题。 – Anupdas 2013-05-13 06:44:30

+0

我觉得答案没有什么不对,很想知道为什么它被拒绝投票。但关于我的问题,我不需要一个dateString我只需要格式。 – Jacob 2013-05-13 11:58:08

0
  1. 日期格式是取决于你如何使用格式化NSDateFormatter你的约会对象。除非格式化日期,否则日期没有格式。

    日期格式

    NSDateFormatterShortStyle 
    NSDateFormatterMediumStyle 
    NSDateFormatterLongStyle 
    

    或使用您自己的格式

    [dateFormatter setDateFormat:@"hh:mm dd.MM.yyyy"]; 
    
  2. 使用相同的日期格式来格式化时间也

    [dateFormatter setTimeStyle: NSDateFormatterShortStyle]  
    
  3. 您可以设置上午/下午符号作为什么ev呃你想

    [dateFormatter setAMSymbol:@"am"]; 
        [dateFormatter setPMSymbol:@"pm"]; 
    
0

可以使用获得当前区域:

NSLocale* currentLocale = [NSLocale currentLocale]; 

,也可以使用例如下面的代码自定义日期时间:

NSDate * date = [m_DateTimePicker date]; 
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]; 
//dd for date, MMMM: month, yyyy:year hh: hour, mm: minute a: AM/PM 
[dateFormatter setDateFormat:@"dd MMMM yyyy hh:mm a"]; 
NSString *convertedString = [dateFormatter stringFromDate:date]; 
9

的方法你正在寻找的是+[NSDateFormatter dateFormatFromTemplate:options:locale:]

此方法为,具体为用于将日期格式“本地化”为所需的语言环境。

例如,如果你想知道用户的首选年 - 月 - 日的格式,你这样做:

NSString *fmt = [NSDateFormatter dateFormatFromTemplate:@"dMMMMy" options:0 locale:[NSLocale currentLocale]]; 

或者,如果你想知道他们的首选时间格式,你这样做:

NSString *fmt = [NSDateFormatter dateFormatFromTemplate:@"jm" options:0 locale:[NSLocale currentLocale]]; 

你用这个方法的时候需要的实际格式字符串。如果你只想根据预定义的样式(长,满,中,短)来格式化一个日期,那么你可以使用其他几个答案中解释的常量。

相关问题