转换日期格式返回错误的日期 xcode

用户6788419

我想将日期从 转换23 May, 201723-05-2017.

我已尝试使用以下代码,但它返回25-12-2016.

NSDateFormatter *oldFormatter = [NSDateFormatter new];
[oldFormatter setDateFormat:@"dd MMM, YYYY"];
NSDate *oldDate = [oldFormatter dateFromString:dateStr];

NSDateFormatter *newFormatter = [NSDateFormatter new];
[newFormatter setDateFormat:@"dd-MM-YYYY"];

我正在使用 Xcode 8.2.1。谢谢你。

尼尔马尔辛

您需要进行以下更改:

YYYY --> YYYY

更新代码:

    NSString *strInputDateString = @"23 May, 2017";
    NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
    [dateFormat setDateFormat:@"dd MMM, yyyy"];

    //Set new dateFormate
//    23-05-2017
    NSDate *date1 = [dateFormat dateFromString:strInputDateString];
    [dateFormat setDateFormat:@"dd-MM-yyyy"];

    NSString *strOutputDateString = [dateFormat stringFromDate:date1];
    NSLog(@"%@",strInputDateString);
    NSLog(@"%@",strOutputDateString);

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章