NSDateFormatter 字符串
以下日期/时间的 NSDateFormatter
字符串是什么: 02/22/2011 10:43:00 AM
基本上我想要的是读取字符串 02 /22/2011 19:00:23 PM
,将其转换为NSDate
,然后打印即可。我之前的尝试:
NSDateFormatter* dtFormatter = [[NSDateFormatter alloc] init];
[dtFormatter setDateFormat:@"MM/dd/yyyy HH:mm:ss a"];
NSDate* dt3 = [dtFormatter dateFromString:@"02/22/2011 19:00:23 PM"];
NSString* strDate3 = [dtFormatter stringFromDate:dt3];
NSLog(@"Third = %@",strDate3);
每次我尝试打印 NULL 时,当我删除尾随的 PM 时它就起作用了。
最后,我在上面代码的第二行之后添加了以下几行:
[dtFormatter setPMSymbol:@"PM"];
[dtFormatter setAMSymbol:@"AM"];
What is the NSDateFormatter
string for the following date/Time : 02/22/2011 10:43:00 AM
Basically what I wanted was to read a string 02/22/2011 19:00:23 PM
, convert it to NSDate
and then print is appropriately. My previous attempt:
NSDateFormatter* dtFormatter = [[NSDateFormatter alloc] init];
[dtFormatter setDateFormat:@"MM/dd/yyyy HH:mm:ss a"];
NSDate* dt3 = [dtFormatter dateFromString:@"02/22/2011 19:00:23 PM"];
NSString* strDate3 = [dtFormatter stringFromDate:dt3];
NSLog(@"Third = %@",strDate3);
Every time I tried that it would print NULL, it worked when I removed the trailing PM.
Finally, I added the following lines right after the 2nd line in the code above:
[dtFormatter setPMSymbol:@"PM"];
[dtFormatter setAMSymbol:@"AM"];
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
有关更多信息,请参阅:http://unicode.org/reports/tr35/tr35- 6.html#Date_Format_Patterns
Fore more info see: http://unicode.org/reports/tr35/tr35-6.html#Date_Format_Patterns
尝试一下,这将为您提供所需的格式。
如果您想要来自 NSDate 对象的字符串,这是正确的方法...现在尝试一下,祝你
好运!
Try it this will give you the required format..
This is the correct way if you want string from NSDate object...Now try it
Good Luck!