【问题标题】:Set time 00:00:00 in NSDate when timezone is not Local当时区不是本地时,在 NSDate 中设置时间 00:00:00
【发布时间】:2016-04-21 07:34:48
【问题描述】:

我正在开发在不同国家/地区拥有用户的应用程序。例如,印度和美国。

我希望 NSDate 在任何时区都没有时间。

例如,2016-03-01 在两个时区 2016-03-01 00:00:00 +0000 中将保持不变。

我正在使用

NSDate* dateOnly = [[NSCalendar currentCalendar] dateBySettingHour:0 minute:0 second:0 ofDate:[NSDate date] options:NSCalendarMatchLast];

我仍然得到这个: 2016-04-21 05:00:00 +0000

我需要 2016-04-21 00:00:00 +0000

请提出适当的解决方案。提前致谢。

【问题讨论】:

    标签: ios objective-c datetime nsdate ios9


    【解决方案1】:

    这个应该可以的

    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setTimeZone:[NSTimeZone timeZoneWithName:@"UTC"]];
    NSLog(@"%@", [dateDormatter stringFromDate:dateOnly]);
    

    【讨论】:

      【解决方案2】:
      NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
      dateFormatter.dateFormat = @"yyyy-MM-dd'T'HH:mm";
      
      NSTimeZone *gmt = [NSTimeZone timeZoneWithAbbreviation:@"GMT"];
      [dateFormatter setTimeZone:gmt];
      NSString *timeStamp = [dateFormatter stringFromDate:[NSDate date]];
      

      试试上面的代码行。希望它会帮助你。

      【讨论】:

      • 我需要 NSDate,而不是 NSString。
      • 您可以将日期转换为字符串
      【解决方案3】:

      你可以试试吗?

      NSTimeZone *timeZone = [NSTimeZone timeZoneWithName:@"UTC"];
      [dateFormatter setTimeZone:timeZone];

      hope help you this

      【讨论】:

        猜你喜欢
        • 2013-07-23
        • 1970-01-01
        • 2017-09-10
        • 1970-01-01
        • 1970-01-01
        • 2014-12-11
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多