【问题标题】:iOS : Switching between 12/24 hour times from stringsiOS:从字符串在 12/24 小时之间切换
【发布时间】:2013-08-17 17:25:28
【问题描述】:

引起我注意的有趣问题。

我收到从服务器到设备的字符串时间。然后我将其转换为 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];

【问题讨论】:

  • self.startTime 从何而来?
  • 开始时间设置为@"14:00"

标签: ios objective-c nsdate nsdateformatter


【解决方案1】:

很接近了……我想你需要:

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];

【讨论】:

    【解决方案2】:

    你可以试试这个

        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]];
    

    【讨论】:

    • 假设我的日期是 2016-11-21 21:25:35,我希望它是上午 10.55,但在你的代码中我得到了上午 2:55
    • 你确定。在我的 php 数据库中 { "date_create" = "2016-11-28 20:21:59";从 = 到; "is_offer" = 0; “job_id”=0; message = "检查并计时系统时间上午 9:51"; "message_id" = 86; "message_ref" = 83; "message_status" = 发送; “发件人 ID” = 1;状态 = 1; “治疗师_id”= 6; } 这个数据,我只是使用你的代码来获取聊天时间,当我转换它时,我得到了 8:21 PM
    • @HimanshuMoradiya。那是正确的输出。该代码将“date_create”=“2016-11-28 20:21:59”(其时间采用 hh:mm:ss 格式)转换为 8:21 PM(采用 hh:mm 格式)。顺便说一句,您可以根据需要更新时间格式。
    • 现在我只是用一些不同的风格解决了我的问题。你的答案显示正确,但我的 php 方面有问题。
    【解决方案3】:

    你必须添加上午/下午:

    NSLocale *twelveHourLocale = [[NSLocale alloc] initWithLocaleIdentifier:@"en_US_POSIX"];
    dateFormat.locale = twelveHourLocale;
    [dateFormat setDateFormat:@"hh:mm: a"];
    

    【讨论】:

    • 我确实尝试过,但仍然给我 null
    • 尝试添加 ss 并且仍然为 null 。字符串是 @"14:00" 进入 hh:mm:ss a 和 null 出来
    • @Burf2000 您尝试添加 NSLocale 了吗?
    【解决方案4】:

    将 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.
    

    【讨论】:

    • 出于某种原因,您的代码是唯一对我有用的代码。谢谢!
    【解决方案5】:

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

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

    【讨论】:

      【解决方案6】:
      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];
      

      【讨论】:

        【解决方案7】:
           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]);
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2020-04-23
          • 1970-01-01
          • 1970-01-01
          • 2018-01-21
          • 1970-01-01
          • 1970-01-01
          • 2021-12-24
          • 1970-01-01
          相关资源
          最近更新 更多