转换NSString-> NSDate?

时间:2011-08-22 22:43:33

标签: ios nsstring nsdate

我需要将我的NSString最终转换为NSDate,但我遇到了问题。如何使最终结果成为NSDate,我将如何使用此代码实现相同的功能?

//Date and Format it
NSDate *now = [NSDate date];
NSDateFormatter* formatter = [[[NSDateFormatter alloc] init] autorelease];
[formatter setDateFormat:@"MM-dd-yyyy"];
NSString *dateString = [formatter stringFromDate:now];

谢谢!

编辑:

//Date and Format it
    NSDate *now = [NSDate date];
    NSDateFormatter* formatter = [[[NSDateFormatter alloc] init] autorelease];
    [formatter setDateFormat:@"MM-dd-yyyy"];
    NSString *dateString = [formatter stringFromDate:now];
    NSDate *enddate = [formatter dateFromString:dateString];
    [[NSUserDefaults standardUserDefaults] setObject:enddate forKey:@"theDate"];

1 个答案:

答案 0 :(得分:3)

NSDate *date = [formatter dateFromString:dateString];