简体   繁体   English

将ISO-8601日期字符串重新格式化为我自己的格式

[英]Reformatting an ISO-8601 date string to my own format

I have this date string - "2011-07-04T00:00:00" , and I would like to display the date as "July 7, 2011" 我有此日期字符串- "2011-07-04T00:00:00" ,我想将日期显示为"July 7, 2011"

This is the code I'm using to convert the date string to the formatted date string - 这是我用来将日期字符串转换为格式化的日期字符串的代码-

NSDateFormatter* formatter = [[NSDateFormatter alloc]init];
[formatter setDateFormat:@"yyyy-MM-dd'T'HH:mm:SS"];
NSDate *date = [f dateFromString:@"2011-07-04T00:00:00"];
[formatter setDateStyle:NSDateFormatterLongStyle];
NSString* formattedDate = [formatter stringFromDate: date];

Is this the best way? 这是最好的方法吗?

its is always better to use system format for date to prevent localization issues . 为防止本地化问题,最好使用系统格式作为日期。

Different countries and regions have different conventions for formatting numerical and time-based information. 不同的国家和地区在格式化数字和基于时间的信息方面有不同的约定。 Locale settings provide information about the formats used by the current user and must be considered when writing code that handles user-facing data types. 语言环境设置提供有关当前用户使用的格式的信息,并且在编写处理面向用户数据类型的代码时必须考虑它们。 The user sets the locale by choosing a region in Settings on iOS devices and System Preferences on a Mac. 用户通过在iOS设备上的“设置”和Mac上的“系统偏好设置”中选择一个区域来设置区域设置。 The user can also change locale settings while the app is running. 用户还可以在应用运行时更改区域设置。 Therefore, if you manipulate data objects in your code and then present them to the user, use the locale APIs to format the data correctly. 因此,如果您在代码中处理数据对象,然后将其呈现给用户,请使用区域设置API正确格式化数据。

https://developer.apple.com/library/prerelease/ios/documentation/MacOSX/Conceptual/BPInternational/InternationalizingLocaleData/InternationalizingLocaleData.html https://developer.apple.com/library/prerelease/ios/documentation/MacOSX/Conceptual/BPInternational/InternationalizingLocaleData/InternationalizingLocaleData.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM