来自设备的日期格式基于区域设置 [英] Date formats from device based on locale

查看:115
本文介绍了来自设备的日期格式基于区域设置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

据我所知,日期格式会因区域设置而异,
有没有办法从当前日期的设备本地设置中读取日期格式?

As per my knowledge the date format will vary based on locale, Is there a way to read date-format from device's local settings for current date?


  1. 日期格式是dd / MM / yyyy还是MM / dd / yyyy。

  2. 时间格式为h:mm a或其他。

  3. AM / PM文本为AM或PM。

我买不起任何第三方图书馆,请告诉我这是否可以使用任何苹果课程来完成。

I can't afford for any third party libraries, please tell me whether this can be done using any apple classes.

编辑:

我不想设置任何格式,我只想根据语言环境从设备中检索它。

I don't want to set any formats, i just want to retrieve it from device based on locale.

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

say, here in my country the date format is dd/MM/yyyy in some part of the word its MM/dd/yyyy i want to get this format.

任何答案都表示赞赏。

推荐答案

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

I thought, this would be just a simple fix. but on seeing some cool answers to this question, went curious about it. Here is my solution.

您可以使用NSDateFormatter,将样式设置为所需格式

You can use NSDateFormatter for this, set the style to required formats

NSDateFormatterShortStyle   
NSDateFormatterMediumStyle
NSDateFormatterLongStyle

了解更多请查看文档

以下是适合您的代码段。

Below is the snippet for you.

    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

它将使用用户的语言环境格式。希望能解决你的问题。

it will use the user's locale formats. Hope that solves your problem.

这篇关于来自设备的日期格式基于区域设置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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