iphone sdk:如何将阿拉伯日期格式转换为英文?

发布于 2024-08-20 17:41:09 字数 139 浏览 5 评论 0原文

我正在开发一个应用程序,其中我将当前日期保存在数据库中,但是当我的应用程序以阿拉伯语运行时,当前日期格式将更改为阿拉伯语。我的意思是日期格式应该像这样 09/02/2010 但数字转换为阿拉伯数字。那么,即使我的应用程序以阿拉伯语运行,如何将它们转换回英语数字呢?

I am working on an application in which I save the current Date in the database but when my app runs in Arabic language the current date format is changed into Arabic. I mean the date format should be like this 09/02/2010 but the digits are converted to Arabic digits. So how do I convert them back to English digits even if my app running in the Arabic Language?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

身边 2024-08-27 17:41:09

一般来说,最好将日期保留为不可知类型,并且仅在必须显示时才对其进行格式化。常见格式是存储自 1970 年或您选择的其他日期以来的秒数。

例如,以下代码将为本地用户显示正确格式的当前时间。

NSDate* now = [NSDate dateWithTimeIntervalSinceNow:0];
NSDateFormatter* formatter = [[NSDateFormatter alloc] init];
[formatter setLocale:[NSLocale currentLocale]];
NSString* localDateString = [formatter stringFromDate];
[formatter release];

但是,当您确实有特定于区域设置的格式的日期时,您可以再次使用格式化程序类来转换它。例如,

NSString* localDate = @"09/02/2010";  // assume this is your string

NSDateFormatter* formatter = [[NSDateFormatter alloc] init];
[formatter setLocale:[NSLocale currentLocale]];
NSDate* date = [formatter dateFromString: localDate];
[formatter release];

对于处理任何类型的特定于区域设置的数据(日期、货币、测量值),那么格式化程序类就是您的朋友。

In general it's best to keep dates in an agnostic type and only format them when they must be displayed. A common format is storing the number of seconds since 1970, or another date you choose.

E.g. the following code will display the current time correctly formatted for the users local.

NSDate* now = [NSDate dateWithTimeIntervalSinceNow:0];
NSDateFormatter* formatter = [[NSDateFormatter alloc] init];
[formatter setLocale:[NSLocale currentLocale]];
NSString* localDateString = [formatter stringFromDate];
[formatter release];

However when you do have a date in a locale-specific format, you can again use the formatter class to convert it. E.g.

NSString* localDate = @"09/02/2010";  // assume this is your string

NSDateFormatter* formatter = [[NSDateFormatter alloc] init];
[formatter setLocale:[NSLocale currentLocale]];
NSDate* date = [formatter dateFromString: localDate];
[formatter release];

For working with any type of locale-specific data (dates, currency, measurements) then the formatter classes are your friend.

动次打次papapa 2024-08-27 17:41:09

我在这里找到了它,我是这样做的

NSDate *CurrentDate = [NSDate date];
NSDateFormatter *Formatter=[[NSDateFormatter alloc] init];
NSLocale *indianLocale = [[NSLocale alloc] initWithLocaleIdentifier:@"en_US"];
[Formatter setLocale:indianLocale];
[Formatter setDateFormat:@"dd/MM/yyyy"];
NSString *FormattedDate=[Formatter stringFromDate:CurrentDate];
[indianLocale release];
[Formatter release];

i found it here i did it like this

NSDate *CurrentDate = [NSDate date];
NSDateFormatter *Formatter=[[NSDateFormatter alloc] init];
NSLocale *indianLocale = [[NSLocale alloc] initWithLocaleIdentifier:@"en_US"];
[Formatter setLocale:indianLocale];
[Formatter setDateFormat:@"dd/MM/yyyy"];
NSString *FormattedDate=[Formatter stringFromDate:CurrentDate];
[indianLocale release];
[Formatter release];
日裸衫吸 2024-08-27 17:41:09

试试这个

NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
NSTimeZone *timeZone = [NSTimeZone timeZoneWithName:@"EST"];
[dateFormatter setTimeZone:timeZone];
[dateFormatter setDateFormat:@"YYYY-MM-dd HH:mm:ss"];
NSString *dateString = [dateFormatter stringFromDate:localDate];

Try this

NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
NSTimeZone *timeZone = [NSTimeZone timeZoneWithName:@"EST"];
[dateFormatter setTimeZone:timeZone];
[dateFormatter setDateFormat:@"YYYY-MM-dd HH:mm:ss"];
NSString *dateString = [dateFormatter stringFromDate:localDate];
北音执念 2024-08-27 17:41:09

斯威夫特

dateFormatter.locale = NSLocale(localeIdentifier: "en_US") as Locale!
dateFormatter.dateFormat = "dd/MM/yyyy"
let resultsDate = dateFormatter.string(from: date)

Swift

dateFormatter.locale = NSLocale(localeIdentifier: "en_US") as Locale!
dateFormatter.dateFormat = "dd/MM/yyyy"
let resultsDate = dateFormatter.string(from: date)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文