2016-01-21 52 views
-1

下面是我的示例代码。日期值延迟一天的原因是什么?

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。延迟一天的原因是什么?请帮帮我。

+1

你的时区是什么? – Thilo

+0

时区:印度。日期是固定的,但结果记录器打印是一天延迟。是什么原因。任何时区使用但日期是不变的字符串值。 – Baskar

+1

那么,印度是+5:30左右。所以Jan,17,00:00你将在1月16日18:30在格林威治。您必须让格式化程序在您的时区打印。 – Thilo

回答

1
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); 
2

您不应该在没有时区标记的情况下从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 
    
+0

我的日期是contstant字符串。延迟一天的原因是什么? – Baskar

+0

周一UTC时间为23:00的一些示例: 埃及开罗:UTC + 02;周二01:00 惠灵顿,新西兰:UTC + 12;星期二11:00我的系统和本地时区亚洲/ kolkata(GMT + 5.30)星期二 – Justlike

+0

。 – Baskar

0

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

我强迫日期在前2个方案中为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 

希望它可以帮助你......!