我想转换日期23 May, 2017
to 23-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);
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)