【问题标题】:what is the reason for one day delay in date value?日期值延迟一天的原因是什么?
【发布时间】:2016-01-21 11:19:55
【问题描述】:

下面是我的示例代码。

NSDateFormatter * dateFormatter = [[NSDateFormatter alloc]init];
    [dateFormatter setDateFormat:@"yyyy-MM-dd"];
    NSDate * dateFormatted = [dateFormatter dateFromString:@"2016-01-17"];
NSLog(@"Date : %d",dateFormatted);

输出为 2016-01-16 18:30:00 +0000。如何获取日期格式 2016-01-17 。延迟一天的原因是什么。请帮帮我。

【问题讨论】:

  • 你的时区是?
  • 时区:印度。上午日期是固定的,但结果记录器打印延迟一天。是什么原因。任何时区使用,但日期是常量字符串值。
  • 嗯,印度是 +5:30 左右。所以 Jan, 17, 00:00 对你来说将是 1 月 16 日 18:30 在格林威治。您必须告诉格式化程序在您的时区打印。
  • 一个 NSDate 只是一个瞬间,从 1970 年 1 月 1 日开始计算秒数。如果您打印它,它将以 UTC 显示它的值。
  • Baskar,你应该知道的第一件事是印度比格林威治标准时间早 5 1/2 小时,所以印度 17 日午夜 18:30格林威治标准时间 16 日。结果绝对正确。

标签: ios objective-c nsdateformatter nstimezone


【解决方案1】:

您不应该从没有时区标记的 NSString 创建 NSDate。

  1. 将时区添加到 NSString like "-0800"

    e.g. "2016-01-17 -0800"  --> "yyyy-MM-dd Z"
    
  2. 添加代码:

    [dateFormatter setTimeZone:[NSTimeZone systemTimeZone]];// change timezone u want
    

【讨论】:

  • 我的日期是常量字符串。延迟一天的原因是什么。
  • UTC 周一 23:00 时的一些示例:埃及开罗:UTC+02;周二 01:00 新西兰惠灵顿:UTC+12;周二 11:00
  • 我的系统和本地时区亚洲/加尔各答 (GMT+5.30)。
  • @Baskar 原来如此。 18:30 + 5:30 = 24:00 ==> 2016-01-16 18:30 +0000 --> 2016-01-17 00:00 +0530
【解决方案2】:
   NSDateFormatter * dateFormatter = [[NSDateFormatter alloc]init];
    [dateFormatter setDateFormat:@"yyyy-MM-dd"];
    NSDate * dateFormatted = [dateFormatter dateFromString:@"2016-01-17"];
    NSTimeZone *currentTimeZone = [NSTimeZone localTimeZone];
    NSInteger currentGMTOffset = [currentTimeZone secondsFromGMTForDate:dateFormatted];
    NSTimeZone *utcTimeZone = [NSTimeZone timeZoneWithAbbreviation:@"UTC"];

    NSInteger gmtOffset = [utcTimeZone secondsFromGMTForDate:dateFormatted];

    NSTimeInterval gmtInterval = currentGMTOffset - gmtOffset;
    NSDate *destinationDate = [[NSDate alloc] initWithTimeInterval:gmtInterval sinceDate:dateFormatted] ;

    NSLog(@"Date : %@",destinationDate);

【讨论】:

    【解决方案3】:

    您可以使用下面的代码...!

    在前两种情况下,我强制日期为 2016-01-17。对你有用的可以查看使用..!

        NSDateFormatter * dateFormatter = [[NSDateFormatter alloc]init];
        [dateFormatter setDateFormat:@"yyyy-MM-dd"];
        NSDate * sourceDate = [dateFormatter dateFromString:@"2016-01-17"];
    
        int daysToAdd = 1;
        NSDate *presentDate = [sourceDate dateByAddingTimeInterval:60*60*24*daysToAdd]; //Output 2016-01-17 18:30:00 +0000
    
        NSDate * dateFormatted = [dateFormatter dateFromString:@"2016-01-17"];
        NSCalendar *calendar1 = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
        NSDateComponents *components1 = [calendar1 components:NSCalendarUnitYear|NSCalendarUnitMonth|NSCalendarUnitDay fromDate:dateFormatted];
        [components1 setHour:24];//24
        [components1 setMinute:00];//00
        NSDate *presentDate1 = [calendar1 dateFromComponents:components1]; //Output 2016-01-17 18:30:00 +0000
    
        //The input
        NSString *myDateAsAStringValue = @"2016-01-17";
    
        NSDateFormatter *df = [[NSDateFormatter alloc] init];
        [df setTimeZone:[NSTimeZone timeZoneWithAbbreviation:@"UTC"]];
        [df setDateFormat:@"yyyy-MM-dd"];
    
        //Getting Date
        NSDate *myDate = [df dateFromString: myDateAsAStringValue]; // Output 2016-01-17 00:00:00 +0000
    
        //create the formatter for the output
        NSDateFormatter *out_df = [[NSDateFormatter alloc] init];
        [out_df setDateFormat:@"yyyy-MM-dd"];
    
        NSString *dateStr=[out_df stringFromDate:myDate];//2016-01-17
    

    希望对您有所帮助...!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-05-31
      • 1970-01-01
      • 2021-10-30
      • 1970-01-01
      • 1970-01-01
      • 2015-02-28
      相关资源
      最近更新 更多