【问题标题】:Getting NSDate from Seconds in ObjC [duplicate]从 ObjC 中的秒数获取 NSDate [重复]
【发布时间】:2013-03-07 04:05:22
【问题描述】:

我的秒数是 54000。 我怎样才能得到下午 3:00:00 的时间?

我可以使用任何日期格式化程序来获取它吗?

谢谢

【问题讨论】:

  • 时区更改会发生什么?
  • 这不是完全相同的副本,但该问题中提供的答案也适用于此处。基本上你有一个时间间隔,起点是午夜。

标签: objective-c nsdate nsdateformatter


【解决方案1】:

假设给定的秒数在本地时区,并且您想要转换为本地时区。

NSInteger timeInSeconds = 54000;

NSDate *date = [NSDate dateWithTimeIntervalSinceReferenceDate:timeInSeconds]];

NSDateFormatter *formatter = [[NSDateFormatter alloc]init];
[formatter setDateFormat:@"hh:mm:ss a"];

NSString *dateString = [formatter stringFromDate:date];

【讨论】:

    【解决方案2】:

    假设您希望在“正常”日期(即没有更改时区的日期)根据当前区域设置格式化时间,

    double seconds = 54000;
    
    NSDateFormatter * fmt = [NSDateFormatter new];
    [dateFormatter setDateStyle:NSDateFormatterNoStyle];
    [dateFormatter setTimeStyle:NSDateFormatterLongStyle];
    [dateFormatter setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]];
    
    NSDate *date = [NSDate dateWithTimeIntervalSinceReferenceDate:seconds];
    NSString * s = [fmt stringFromDate:date];
    
    // If you are not using ARC,
    [fmt release];
    

    【讨论】:

      猜你喜欢
      • 2015-03-04
      • 1970-01-01
      • 2015-12-28
      • 1970-01-01
      • 2012-12-01
      • 1970-01-01
      • 1970-01-01
      • 2021-03-19
      • 1970-01-01
      相关资源
      最近更新 更多