不仅仅在ipad中转换24小时格式

时间:2016-11-02 10:36:39

标签: ios iphone ipad nsdateformatter

大家好,在我的项目中,我想转换日期&时间(12小时)到时间(24小时)它在iphone& ipod但我在iPad上运行它没有正确转换是什么问题。

 NSDateFormatter *dateformatter = [[NSDateFormatter alloc] init];
    dateformatter.dateFormat = @"dd-MM-YYYY hh:mm:ss a";
    NSString *st=dictt[@"DATE_TIME"];//29-OCT-2016 09:25:52 AM  -- this is input
    NSDate *date = [dateformatter dateFromString:st];
    NSDateFormatter *dateformatter1 = [[NSDateFormatter alloc] init];
    dateformatter1.dateFormat = @"HH:mm:ss";
    NSString *time24 = [dateformatter1 stringFromDate:date];//9:25:52 am -- this is output

但是在iPhone中它在iphone中正确地给出了9:25:52

2 个答案:

答案 0 :(得分:0)

您需要根据https://developer.apple.com/library/content/qa/qa1480/_index.html

设置区域设置
NSDateFormatter *dateformatter = [[NSDateFormatter alloc] init];
dateformatter.dateFormat = @"dd-MM-yyyy hh:mm:ss a";

// set locale
dateformatter.locale = [NSLocale localeWithLocaleIdentifier:@"en_US_POSIX"]; 

NSString *st=dictt[@"DATE_TIME"];//29-OCT-2016 09:25:52 AM  -- this is input
NSDate *date = [dateformatter dateFromString:st];
NSDateFormatter *dateformatter1 = [[NSDateFormatter alloc] init];
dateformatter1.dateFormat = @"HH:mm:ss";
NSString *time24 = [dateformatter1 stringFromDate:date];//9:25:52 am -- this is output

答案 1 :(得分:0)

我找到了解决方案

Formatter.locale = NSLocale.systemLocale()

这会对你有所帮助