Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/iphone/37.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/94.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/cmake/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Iphone 如何以am/pm格式获取时间?_Iphone_Ios_Time_Nsdateformatter - Fatal编程技术网

Iphone 如何以am/pm格式获取时间?

Iphone 如何以am/pm格式获取时间?,iphone,ios,time,nsdateformatter,Iphone,Ios,Time,Nsdateformatter,我使用NSDateFormatter在iPhone应用程序中工作。我从网络服务中获取时间,如“00:00:00”、“16:00:00”、“15:30:00”等等。我需要把这些时间转换成 如果时间是“00:00:00”到“12 AM”,如果时间是“16:00:00”到“4 PM”,如果时间是“15:30:00”到“3.30 PM”。我使用了下面的代码,但当我通过“16:00:00”时,日期返回null NSString *dats1 = @"16:00:00"; NSDateFormatter

我使用NSDateFormatter在iPhone应用程序中工作。我从网络服务中获取时间,如
“00:00:00”、“16:00:00”、“15:30:00”等等。我需要把这些时间转换成
如果时间是
“00:00:00”到“12 AM”
,如果时间是
“16:00:00”到“4 PM”
,如果时间是“15:30:00”到“3.30 PM”。我使用了下面的代码,但当我通过
“16:00:00”时,日期返回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];

当am通过“00:00:00”时,它将正确返回“12 am”。谁能帮我解决这个问题?提前感谢。

为什么要再次分配
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);

如果您的时间格式为
24小时
,请使用
“HH:mm:ss”
格式。请测试代码,让我知道它是否适合你。我已经测试过了,现在返回
“下午4点”


谢谢。

您的日期格式应为:

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

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

希望有人能帮上忙,希望能在Swift中得到它

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)

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

NSLocale *12hourFomrat = [[NSLocale alloc] initWithLocaleIdentifier:@"en_US_POSIX"];
df.locale = 12hourFomrat;
这始终以24小时格式保存

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

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

let timeFormatter = NSDateFormatter()
timeFormatter.dateStyle = .NoStyle
timeFormatter.timeStyle = .ShortStyle
timeFormatter.stringFromDate(NSDate())
返回
17:12
5:12 PM
,具体取决于当前设备的区域设置。

Swift 5:

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

好代码+1,但我认为首先
dateFormatter setDateFormat:@“HH:mm:ss”]不是必需的。这是必需的,因为他有HH格式的输入小时格式,即24小时格式,这里是12:12am,但返回的是12:12pm。。Hmmmit在16:00返回ios9@PatelJigar添加此[dateFormatter setLocale:[[NSLocale alloc]initWithLocaleIdentifier:@“en_US_POSIX”];
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "hh mm a"
let formattedDate = dateFormatter.string(from: Date())