改变目标C中的格式时间

时间:2015-06-02 08:17:00

标签: ios objective-c nsdate nsdateformatter

我需要更改我的时间格式和我在Google上搜索的日期这是怎么做的,我发现如何使用该代码更改数据格式:

NSString *hora=@"01:32";
NSDateFormatter *formatHora = [[NSDateFormatter alloc] init];
[formatHora setDateFormat:@"hh:mm"];
NSDate *dateHora = [formatHora dateFromString:hora];
[formatHora setDateFormat:@"HH:mm"];
NSString* horaFormatoCorrecto = [formatHora stringFromDate:dateHora];

但我尝试同样的时间和代码不去,我需要将日期的格式更改为24h,我的代码:

NSString *hora=@"01:32";
NSDateFormatter *formatHora = [[NSDateFormatter alloc] init];
[formatHora setDateFormat:@"hh:mm"];
NSDate *dateHora = [formatHora dateFromString:hora];
[formatHora setDateFormat:@"HH:mm"];
NSString* horaFormatoCorrecto = [formatHora stringFromDate:dateHora];

提前致谢。

1 个答案:

答案 0 :(得分:0)

请检查以下代码 -

    NSString *hora=@"01:32 PM";
    NSDateFormatter *formatHora = [[NSDateFormatter alloc] init];
    [formatHora setDateFormat:@"hh:mm a"];
    NSDate *dateHora = [formatHora dateFromString:hora];
    [formatHora setDateFormat:@"HH:mm"];
    NSString* horaFormatoCorrecto = [formatHora stringFromDate:dateHora];