2013-05-10 365 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]]; 

你用這個方法的時候需要的實際格式字符串。如果你只想根據預定義的樣式(長,滿,中,短)來格式化一個日期,那麼你可以使用其他幾個答案中解釋的常量。

相關問題