xcode 如何在 iphone 中以 24 小时格式转换时间?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13023797/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 01:54:09  来源:igfitidea点击:

How to convert time in 24 hr format in iphone?

iphonexcode

提问by 07405

I have to convert the given time in 24 hr format.But I did not get it.Don't know where I m going wrong. If I have TimeStart as 1:15 PM then I should get 13:15 ..

我必须以 24 小时格式转换给定的时间。但我没有得到它。不知道我哪里出错了。如果我将 TimeStart 设为下午 1:15,那么我应该得到 13:15 ..

Item *item=[sectionItems objectAtIndex:itemId];
 NSLog(@"%@",item.TimeStart);
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"HH:mm a"];
    NSString *formattedDateString = [dateFormatter stringFromDate:item.TimeStart];
    NSLog(@"%@",formattedDateString);

In the code above I have a Item class and that I have NSString*TimeStart; When I see formattedDateString it is returning null.How can I fix it ?

在上面的代码中,我有一个 Item 类并且我有NSString*TimeStart; 当我看到 formattedDateString 时,它返回 null。我该如何解决?

回答by Paras Joshi

 Item *item=[sectionItems objectAtIndex:itemId];
  NSLog(@"%@",item.TimeStart);

NSString *str = @"1:45.PM";// put here item.TimeStart

NSDateFormatter *dateFormatter = [[[NSDateFormatter alloc] init] autorelease];
[dateFormatter setDateFormat:@"hh:mm.a"];// here give the format which you get in TimeStart

NSDate *date = [dateFormatter dateFromString: str];

dateFormatter = [[[NSDateFormatter alloc] init] autorelease];
[dateFormatter setDateFormat:@"HH:mm"];

NSString *convertedString = [dateFormatter stringFromDate:date];
NSLog(@"Converted String : %@",convertedString);

回答by Nitin Gohel

i just simply setDateFormat like [formatter setDateFormat:@"HH:mm"];Remove a

我只是简单地 setDateFormat 像[formatter setDateFormat:@"HH:mm"];Removea

and below caode for checking its time formate is 24 hr or 12

和下面的 caode 检查其时间格式是 24 小时或 12

NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setLocale:[NSLocale currentLocale]];
[formatter setDateStyle:NSDateFormatterNoStyle];
[formatter setTimeStyle:NSDateFormatterShortStyle];
NSString *dateString = [formatter stringFromDate:[NSDate date]];
NSRange amRange = [dateString rangeOfString:[formatter AMSymbol]];
NSRange pmRange = [dateString rangeOfString:[formatter PMSymbol]];
BOOL is24h = (amRange.location == NSNotFound && pmRange.location == NSNotFound);
[formatter release];
NSLog(@"%@\n",(is24h ? @"YES" : @"NO"));

回答by iPhone Programmatically

Use this to set 24 hr format.

使用它来设置 24 小时格式。

[currentDate setDateFormat:@"yyyy-MM-dd HH:mm"];

or

或者

[formatter setDateFormat:@"HH:mm"];

without using formatter

不使用格式化程序

NSRange range;
range.location = 2;
range.length = 2;
NSString *dateString=[NSString stringWithFormat:@"%@:%@",[myString substringToIndex:2],          [myString substringWithRange:range]];

This is the code i am using in my project and its working for me

这是我在我的项目中使用的代码,它对我有用

NSDateFormatter *dateFormatter = [[[NSDateFormatter alloc] init] autorelease];
[dateFormatter setDateFormat:@"HH:mm"];
 NSString *dateString = [dateFormatter stringFromDate:[NSDate date]];

NSLog(@"Current time date:%@", dateString);

output:Current time date:14:41

输出:当前时间日期:14:41