NSDateFormatter 字符串

发布于 2024-10-18 11:37:09 字数 654 浏览 0 评论 0原文

以下日期/时间的 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 技术交流群。

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

发布评论

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

评论(2

嘦怹 2024-10-25 11:37:09
MM/dd/Y hh:mm:ss a

有关更多信息,请参阅:http://unicode.org/reports/tr35/tr35- 6.html#Date_Format_Patterns

纸伞微斜 2024-10-25 11:37:09

尝试一下,这将为您提供所需的格式。

NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@"MM/dd/yyyy HH:mm:ss aaa"];

// Conversion of NSString to NSDate
NSDate *dateFromString = [[NSDate alloc] init];
dateFromString = [formatter dateFromString:curDate];   //give what u want to give curDate
[formatter release];

// Conversion of NSDate to NSString
NSString *dateString = [formatter stringFromDate:datePicker.date]; //give date here
[formatter release];

如果您想要来自 NSDate 对象的字符串,这是正确的方法...现在尝试一下,祝你

好运!

Try it this will give you the required format..

NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@"MM/dd/yyyy HH:mm:ss aaa"];

// Conversion of NSString to NSDate
NSDate *dateFromString = [[NSDate alloc] init];
dateFromString = [formatter dateFromString:curDate];   //give what u want to give curDate
[formatter release];

// Conversion of NSDate to NSString
NSString *dateString = [formatter stringFromDate:datePicker.date]; //give date here
[formatter release];

This is the correct way if you want string from NSDate object...Now try it

Good Luck!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文