ios 如何获得时间在上午/下午格式的iPhone NSDateFormatter?

9nvpjoqh  于 2022-12-30  发布在  iOS
关注(0)|答案(8)|浏览(201)

我在使用NSDateFormatter的iPhone应用程序中工作。我从"00:00:00", "16:00:00","15:30:00" and so on这样的Web服务中获取时间。我需要将这些时间转换为如果时间是"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”。有人能帮我解决这个问题吗?

hwamh0ep

hwamh0ep1#

如果您的时间格式是24hrs,请使用"HH:mm:ss"。请测试代码,并让我知道它是否为您工作。我已经测试,它返回"4 PM"

NSString *dats1 = @"16:00:00";
 NSDateFormatter *dateFormatter = [[[NSDateFormatter alloc] init] autorelease];
 [dateFormatter3 setDateStyle:NSDateFormatterMediumStyle];  
 [dateFormatter3 setDateFormat:@"HH:mm:ss"]; 
 NSDate *date1 = [dateFormatter3 dateFromString:dats1];
 NSLog(@"date1 : %@", date1); **// Here returning (null)**

 [dateFormatter setDateFormat:@"hh:mm a"];
 NSLog(@"Current Date: %@", [formatter stringFromDate:date1]); **// Here also returning (null)**
 [formatter release];

谢谢。

gijlo24d

gijlo24d2#

您可以使用.ShortStyle,它将适应当前的语言环境:

let timeFormatter = NSDateFormatter()
timeFormatter.dateStyle = .NoStyle
timeFormatter.timeStyle = .ShortStyle
timeFormatter.stringFromDate(NSDate())

它根据当前设备区域设置返回17:125:12 PM

b5lpy0ml

b5lpy0ml3#

为什么再次分配NSDateFormatter
你只需要把它格式化为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);
qmelpv7a

qmelpv7a4#

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

wnavrhmk

wnavrhmk5#

希望有人会有帮助谁想得到它在斯威夫特

SWIFT 5.1+

let dateFormatter = DateFormatter()
//if you want 12AM,5PM then use "hh mm a"
//if you want 00AM,17PM then use "HH mm a"
dateFormatter.dateFormat = "hh mm a"
let stringDate = dateFormatter.string(from: Date())
print(stringDate)
hs1ihplo

hs1ihplo6#

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

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

这始终保持在24小时格式

NSLocale *24hourFormat = [[NSLocale alloc] initWithLocaleIdentifier:@"en_GB"];
df.locale = 24hourFormat
yquaqz18

yquaqz187#

日期格式应为:

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

检查日期格式模式Doku

smdnsysy

smdnsysy8#

雨燕5:

let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "hh mm a"
let formattedDate = dateFormatter.string(from: Date())

相关问题