简体   繁体   English

来自字符串的简单NSDate

[英]simple NSDate from string

all I want is a date like this: 01/06/1973 as an NSDate. 我想要的只是一个这样的日期:1973年6月1日作为NSDate。

I assumed this would do the trick: 我以为这可以解决问题:

NSDateFormatter *dateFormatter = [[[NSDateFormatter alloc] init] autorelease];
            [dateFormatter setDateFormat:@"yyyy-MM-dd hh:mm:ss"];
            NSDate *myDate =[dateFormatter dateFromString:@"01/06/1973" ];

but my formatting is not being followed 但没有遵循我的格式

Your code would need to be slighty modified, like this: 您的代码需要稍作修改,如下所示:

NSDateFormatter *dateFormatter = [[[NSDateFormatter alloc] init] autorelease];
            [dateFormatter setDateFormat:@"MM/dd/yyyy"];
            oldestDate =[dateFormatter dateFromString:@"01/06/1973" ];

Hope it helps! 希望能帮助到你!

NSDateFormatter *dateFormatter = [[[NSDateFormatter alloc] init] autorelease];
[dateFormatter setDateFormat:@"dd/MM/yyyy"];
NSDate *myDate =[dateFormatter dateFromString:@"01/06/1973" ];

您是否尝试过@“ mm / dd / yyyy hh:mm:ss”作为格式字符串?

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

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