Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 目标C-从12版转换为24版的时间_Ios_Objective C_Iphone_Nsdate_Nsdateformatter - Fatal编程技术网

Ios 目标C-从12版转换为24版的时间

Ios 目标C-从12版转换为24版的时间,ios,objective-c,iphone,nsdate,nsdateformatter,Ios,Objective C,Iphone,Nsdate,Nsdateformatter,我正在使用下面的代码将时间从12转换为24格式,工作正常,但结果比实际时间少了3小时! i、 e:如果时间是下午12:40,我转换成24格式后会得到09:40 NSDateFormatter* df = [[NSDateFormatter alloc]init]; [df setLocale:[[NSLocale alloc] initWithLocaleIdentifier:@"en_US_POSIX"]]; [df setTimeZone:[NSTimeZone systemTimeZone

我正在使用下面的代码将时间从12转换为24格式,工作正常,但结果比实际时间少了3小时! i、 e:如果时间是下午12:40,我转换成24格式后会得到09:40

NSDateFormatter* df = [[NSDateFormatter alloc]init];
[df setLocale:[[NSLocale alloc] initWithLocaleIdentifier:@"en_US_POSIX"]];
[df setTimeZone:[NSTimeZone systemTimeZone]];    
[df setDateFormat:@"MM/dd/yyyy hh:mm a"];
NSDate* newDate = [df dateFromString:strDate4Convert];    
[df setDateFormat:@"MM/dd/yyyy HH:mm:ss"];    
NSString *newTimeStr = [df stringFromDate:newDate];
提前感谢

尝试将时区设置为UTC或本地时区,而不是系统时区

是的,使用本地时区

NSDateFormatter* df = [[NSDateFormatter alloc]init];
[df setLocale:[[NSLocale alloc] initWithLocaleIdentifier:@"en_US_POSIX"]];
[df setTimeZone:[NSTimeZone localTimeZone]];
[df setDateFormat:@"MM/dd/yyyy hh:mm a"];
NSDate* newDate = [df dateFromString:@"08/30/2016 12:40 am"]; // @"08/30/2016 12:40 pm"
[df setDateFormat:@"MM/dd/yyyy HH:mm:ss"];
NSString *newTimeStr = [df stringFromDate:newDate];
输出:

newTimeStr的打印说明: 2016年8月30日00:40:00

newTimeStr的打印说明:
2016年8月30日12:40:00

查看此欢迎伴侣,快乐编码:
NSDateFormatter* df = [[NSDateFormatter alloc]init];
[df setLocale:[[NSLocale alloc] initWithLocaleIdentifier:@"en_US_POSIX"]];
[df setTimeZone:[NSTimeZone localTimeZone]];
[df setDateFormat:@"MM/dd/yyyy hh:mm a"];
NSDate* newDate = [df dateFromString:@"08/30/2016 12:40 am"]; // @"08/30/2016 12:40 pm"
[df setDateFormat:@"MM/dd/yyyy HH:mm:ss"];
NSString *newTimeStr = [df stringFromDate:newDate];