Converting NSString to NSDate (and back again)

后端 未结 17 2498
-上瘾入骨i
-上瘾入骨i 2020-11-21 23:26

How would I convert an NSString like \"01/02/10\" (meaning 1st February 2010) into an NSDate? And how could I turn the NSDat

17条回答
  •  一向
    一向 (楼主)
    2020-11-22 00:11

    Date to NSString

    NSString *dateString = [NSString stringWithFormat:@"%@",[NSDate date]];
    NSLog(@"string: %@",dateString ); //2015-03-24 12:28:49 +0000
    

    NSString to NSDate

    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateFormat:@"yyyy-MM-dd HH:mm:ss Z"];
    NSDate *date = [formatter dateFromString:dateString];
    NSLog(@"date: %@", date); //015-03-24 12:28:49 +0000
    

提交回复
热议问题