ios 如何获取上午/下午格式的时间 iphone NSDateFormatter?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11608133/
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
How to get the time in am/pm format iphone NSDateFormatter?
提问by Gopinath
Am working in an iPhone app with using the NSDateFormatter. Am getting the time from the webservice like "00:00:00", "16:00:00","15:30:00" and so on
. I need to convert these time to
if the time is "00:00:00" to "12 AM"
, if the time is "16:00:00" to "4 PM"
, if the time is "15:30:00" to "3.30 PM". I have used the below code to this but when i have pass "16:00:00" the date returns null
.
我在使用 NSDateFormatter 的 iPhone 应用程序中工作。我从像"00:00:00", "16:00:00","15:30:00" and so on
. 我需要将这些时间转换为如果时间是"00:00:00" to "12 AM"
,如果时间是"16:00:00" to "4 PM"
,如果时间是“15:30:00”到“3.30 PM”。我已经使用了下面的代码,但是当我通过时"16:00:00" the date returns null
。
NSString *dats1 = @"16:00:00";
NSDateFormatter *dateFormatter3 = [[[NSDateFormatter alloc] init] autorelease];
[dateFormatter3 setDateStyle:NSDateFormatterMediumStyle];
[dateFormatter3 setDateFormat:@"hh:mm:ss"];
NSDate *date1 = [dateFormatter3 dateFromString:dats1];
NSLog(@"date1 : %@", date1); **// Here returning (null)**
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@"hh:mm a"];
NSLog(@"Current Date: %@", [formatter stringFromDate:date1]); **// Here also returning (null)**
[formatter release];
When am passing "00:00:00" it is returning "12 AM" correctly. Can anyone please help me to solve this issue? Thanks in advance.
当我通过“00:00:00”时,它会正确返回“12 AM”。任何人都可以帮我解决这个问题吗?提前致谢。
回答by Yuvaraj.M
If your time format is in 24hrs
please use "HH:mm:ss"
format. Please test the code and let me know if it is working for you. I have tested and it is returning "4 PM"
如果您的时间格式是在24hrs
请使用"HH:mm:ss"
格式。请测试代码,让我知道它是否适合您。我已经测试过了,它正在返回"4 PM"
NSString *dats1 = @"16:00:00";
NSDateFormatter *dateFormatter3 = [[[NSDateFormatter alloc] init] autorelease];
[dateFormatter3 setDateStyle:NSDateFormatterMediumStyle];
[dateFormatter3 setDateFormat:@"HH:mm:ss"];
NSDate *date1 = [dateFormatter3 dateFromString:dats1];
NSLog(@"date1 : %@", date1); **// Here returning (null)**
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@"hh:mm a"];
NSLog(@"Current Date: %@", [formatter stringFromDate:date1]); **// Here also returning (null)**
[formatter release];
Thanks.
谢谢。
回答by eliocs
You can use the .ShortStyle
which will adapt to the current locale:
您可以使用.ShortStyle
将适应当前语言环境的 :
let timeFormatter = NSDateFormatter()
timeFormatter.dateStyle = .NoStyle
timeFormatter.timeStyle = .ShortStyle
timeFormatter.stringFromDate(NSDate())
Which returns 17:12
or 5:12 PM
depending on the current device locale.
返回17:12
或5:12 PM
取决于当前设备区域设置。
回答by Charan
Why are you again allocating the NSDateFormatter
?
你为什么要再次分配NSDateFormatter
?
you Just need to format it to NSDate
and then again to NSString
,
你只需要将它格式化为NSDate
然后再次格式化NSString
,
NSDateFormatter *dateFormatter = [[[NSDateFormatter alloc] init] autorelease];
[dateFormatter setDateFormat:@"HH:mm:ss"];
NSDate *date = [dateFormatter dateFromString:dats1];
[dateFormatter setDateFormat:@"hh:mm a"];
NSString *formattedDate = [dateFormatter stringFromDate:date];
NSLog(@"%@",formattedDate);
回答by Steven Kramer
Try HH:mm:ss
(notice the capitals in the hour format)
尝试HH:mm:ss
(注意小时格式的大写字母)
回答by Rafat touqir Rafsun
Hope someone would be helpful who would like to get it in Swift
希望有人会帮助想在 Swift 中获得它的人
For Swift
对于斯威夫特
let dateFormatter = NSDateFormatter()
//if some one want 12AM,5PM then use "hh mm a"
//if some one want 00AM,17PM then use "HH mm a"
dateFormatter.dateFormat = "hh mm a"
let stringDate = dateFormatter.stringFromDate(NSDate())
println(stringDate)
回答by Mutablegopi
This Below code always keep in 12hour format:
下面的代码始终保持 12 小时格式:
NSLocale *12hourFomrat = [[NSLocale alloc] initWithLocaleIdentifier:@"en_US_POSIX"];
df.locale = 12hourFomrat;
This always keep in 24 hour format
这始终保持在 24 小时格式
NSLocale *24hourFormat = [[NSLocale alloc] initWithLocaleIdentifier:@"en_GB"];
df.locale = 24hourFormat
回答by CarlJ
your date format should be:
你的日期格式应该是:
[dateFormatter3 setDateFormat:@"HH:mm:ss"];
check the Date Format Pattern Doku
回答by Sreedeepkesav M S
Swift 5:
斯威夫特 5:
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "hh mm a"
let formattedDate = dateFormatter.string(from: Date())