php取am+pm时间格式,ios-如何以am / pm格式获取时间iphone NSDateFormatter?

ios-如何以am / pm格式获取时间iphone NSDateFormatter?

我正在使用NSDateFormatter在iPhone应用程序中工作。 我正在从Web服务获取时间,例如"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];

当传递“ 00:00:00”时,它将正确返回“ 12 AM”。 谁能帮我解决这个问题? 提前致谢。

7个解决方案

113 votes

如果您的时间格式为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];

谢谢。

Yuvaraj.M answered 2019-12-31T14:22:22Z

22 votes

您可以使用17:12来适应当前的语言环境:

let timeFormatter = NSDateFormatter()

timeFormatter.dateStyle = .NoStyle

timeFormatter.timeStyle = .ShortStyle

timeFormatter.stringFromDate(NSDate())

根据当前设备的语言环境,将返回17:12或5:12 PM。

eliocs answered 2019-12-31T14:22:47Z

15 votes

为什么还要重新分配NSDate?

您只需要将其格式化为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);

Charan answered 2019-12-31T14:23:11Z

14 votes

尝试HH:mm:ss(注意小时格式的大写字母)

Steven Kramer answered 2019-12-31T14:23:31Z

8 votes

希望有人愿意在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)

Rafat touqir Rafsun answered 2019-12-31T14:23:56Z

8 votes

以下代码始终保持12小时格式:

NSLocale *12hourFomrat = [[NSLocale alloc] initWithLocaleIdentifier:@"en_US_POSIX"];

df.locale = 12hourFomrat;

始终保持24小时制

NSLocale *24hourFormat = [[NSLocale alloc] initWithLocaleIdentifier:@"en_GB"];

df.locale = 24hourFormat

Mutablegopi answered 2019-12-31T14:24:20Z

3 votes

您的日期格式应为:

[dateFormatter3 setDateFormat:@"HH:mm:ss"];

检查日期格式模式Doku

CarlJ answered 2019-12-31T14:24:44Z

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值