15

有趣的问题让我很着迷。

我从服务器接收到设备的字符串时间。然后我将其转换为 NSDate。当设备设置为显示 24 小时时间时,寿命很好。

现在我正在设置为 12 小时的设备上对其进行测试。一切都停止了工作。日期返回为空

我第一次有

 NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
 [dateFormat setDateFormat:@"HH:mm"];
 self.startTime = [dateFormat dateFromString:(NSString *)self.startTime];

非常适合显示 24 小时日期但不是 12 小时的设备。

然后我尝试了

 NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
 [dateFormat setDateFormat:@"hh:mm"];
 self.startTime = [dateFormat dateFromString:(NSString *)self.startTime];

这在中午 12 点之前都可以正常工作,然后所有日期都返回为 null

更新

我也尝试添加“a”,但这仍然导致返回 null

 if (startDate == nil)
 {
      NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
      [dateFormat setDateFormat:@"hh:mm a"];
      startDate = [dateFormat dateFromString:(NSString *)self.startTime];
 }

更新 2

添加本地,添加:ss添加所有仍然不起作用

NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
NSLocale *twelveHourLocale = [[NSLocale alloc] initWithLocaleIdentifier:@"en_US_POSIX"];
dateFormat.locale = twelveHourLocale;
[dateFormat setDateFormat:@"hh:mm a"];
startDate = [dateFormat dateFromString:(NSString *)self.startTime];
4

7 回答 7

18

它很接近......我认为你需要:

NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
NSLocale *locale = [[NSLocale alloc] initWithLocaleIdentifier:@"en_US_POSIX"];
[dateFormatter setLocale:locale];
[dateFormatter setDateFormat:@"HH:mm"];
startDate = [dateFormatter dateFromString:(NSString *)self.startTime];
于 2013-08-15T15:06:55.677 回答
7

你可以试试这个

    12 to 24 hour format


    NSDateFormatter* df = [[NSDateFormatter alloc] init];
    [df setLocale:[[NSLocale alloc] initWithLocaleIdentifier:@"en_US_POSIX"]];
    [df setTimeZone:[NSTimeZone systemTimeZone]];
    [df setDateFormat:@"hh:mm a"];
    NSDate* newDate = [df dateFromString:LocationTrackingStartTime];
    [df setDateFormat:@"HH:mm"];
    newDate = [df stringFromDate:newDate];

   24 to 12 hour format

    NSDateFormatter* df = [[NSDateFormatter alloc] init];
    [df setLocale:[[NSLocale alloc] initWithLocaleIdentifier:@"en_US"]];
    [df setTimeZone:[NSTimeZone systemTimeZone]];
    [df setDateFormat:@"yyyy-mm-dd hh:mm:ss"];
    NSDate* newDate = [df dateFromString:[df stringFromDate:[NSDate date]]];
    [df setDateFormat:@"hh:mm a"];
    newDate = [df stringFromDate:[NSDate date]];
于 2014-01-22T06:27:17.713 回答
4

您必须添加上午/下午:

NSLocale *twelveHourLocale = [[NSLocale alloc] initWithLocaleIdentifier:@"en_US_POSIX"];
dateFormat.locale = twelveHourLocale;
[dateFormat setDateFormat:@"hh:mm: a"];
于 2013-08-15T14:15:47.697 回答
2

将 24 小时格式更改为 12 小时格式,

    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    //Set 24 hours date format
    dateFormatter.dateFormat = @"HH:mm";

    //Here @"your24hours date string" is your string date with 24 hours format
    NSDate *date = [dateFormatter dateFromString:@"your24hours date string"];

    //Set 12 hours date format
    dateFormatter.dateFormat = @"hh:mm a";
    NSString *strDate = [dateFormatter stringFromDate:date];

    NSLog (@"%@", strDate);

   //Output is in 12 hours date format.
于 2015-04-15T14:34:57.847 回答
1

您想将NSDate强制转换NSStringNSDate再次。首先,您必须将其转换为 and NSString,而不是强迫它!

我也支持 12 和 24 小时格式,我对此没有任何问题。

于 2013-08-15T15:03:09.903 回答
1
NSDateFormatter* df = [[NSDateFormatter alloc]init];

[df setLocale:[[NSLocale alloc] initWithLocaleIdentifier:@"en_US"]];
[df setTimeZone:[NSTimeZone systemTimeZone]];    
[df setDateFormat:@"MM/dd/yyyy hh:mm a"];

NSDate* newDate = [df dateFromString:strDate4Convert];    
[df setDateFormat:@"MM/dd/yyyy HH:mm:ss"];    
 NSString *newTimeStr = [df stringFromDate:newDate];
于 2013-12-18T05:40:28.433 回答
1
   You just need to set the locale to take the am pm format. And everything else as usual.

    NSDateFormatter * df = [[NSDateFormatter alloc]init];
    NSLocale *locale = [[NSLocale alloc] initWithLocaleIdentifier:@"en_US_POSIX"]; 
    [df setLocale:locale];
    [df setDateStyle:NSDateFormatterNoStyle];
    [df setTimeStyle:NSDateFormatterShortStyle];
    NSLog(@"%@", [df stringFromDate:pickUpDate]);
于 2014-10-01T06:39:11.023 回答