Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/iphone/40.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Iphone NSdate(时区)中的问题日期时间显示过去时间5小时30分钟_Iphone_Objective C_Nsdate_Nsdateformatter_Nsdatecomponents - Fatal编程技术网

Iphone NSdate(时区)中的问题日期时间显示过去时间5小时30分钟

Iphone NSdate(时区)中的问题日期时间显示过去时间5小时30分钟,iphone,objective-c,nsdate,nsdateformatter,nsdatecomponents,Iphone,Objective C,Nsdate,Nsdateformatter,Nsdatecomponents,实际上,在我的模拟器中,时间显示过去5小时30分钟 我目前在印度的时间是2013-04-02 09:10:_uu_u_u_u_u_u_u_u_u_u_u__________________ 2013年4月2日上午9:10 我需要在两个日期之间得到一些结果1.月的第一个日期到2.任何一天的当前日期 从4月1日到4月2日上午9:10到当前时间2013-04-02 09:10:00 但这很令人失望 月份第一天的日期为2013-03-31 18:30:00+0000 当前时间为2013-04-02 03

实际上,在我的模拟器中,时间显示过去5小时30分钟

我目前在印度的时间是2013-04-02 09:10:_uu_u_u_u_u_u_u_u_u_u_u__________________

2013年4月2日上午9:10

我需要在两个日期之间得到一些结果1.月的第一个日期到2.任何一天的当前日期

从4月1日到4月2日上午9:10到当前时间2013-04-02 09:10:00

但这很令人失望

月份第一天的日期为2013-03-31 18:30:00+0000

当前时间为2013-04-02 03:54:51+0000,但实际时间为上午9:10:00

我计算这个月的第一个日期如下

// TO IMPLEMENT CODE for FIRST DAY OF the Month
      NSCalendar* calendar = [NSCalendar currentCalendar];
      NSDateComponents* comps = [calendar components:NSYearForWeekOfYearCalendarUnit |NSYearCalendarUnit|NSMonthCalendarUnit|NSWeekCalendarUnit|NSWeekdayCalendarUnit fromDate:[NSDate date]];

 calendar.timeZone = [NSTimeZone systemTimeZone];

      [comps setWeekday:1]; // 1: sunday
      monthComponent = comps.month;
      yearComponent = comps.year;
      dateFormatter = [[NSDateFormatter alloc] init];
      [dateFormatter setDateFormat:@"yyyy MM dd"];

 dateFormatter.timeZone = [NSTimeZone systemTimeZone];

      // To get the firstDateOfMonth for This MONTH.
      NSDate *firstDateOfMonth = [dateFormatter dateFromString:[NSString stringWithFormat:@"%d %d 01",yearComponent,monthComponent]];

      NSLog(@"firstDateOfMonth-- %@ \r\n",firstDateOfMonth);
       NSLog(@"[NSDate date]-- %@",[NSDate date]);
我设置了时区SystemTimeZone,甚至认为我在当前日期和每月的第一个日期上得到了错误的结果

提前感谢

@IOS开发人员

可能是时区问题。检查你的模拟器设置。使用以下链接:

还可以使用[NSTimeZone localTimeZone]进行检查

请参阅以下链接:

问题是时区。 默认情况下,NSDateFormatter设置为本地时区。也就是说,如果你的时区是+5:30,那么给它一个日期1970/18/3,结果是1970-03-18 00:00:00+0530。但是,NSLog始终以GMT零时区打印日期,添加/减去时区差5小时30分钟

 NSCalendar* calendar = [NSCalendar currentCalendar];
    NSDateComponents* comps = [calendar components:NSYearForWeekOfYearCalendarUnit |NSYearCalendarUnit|NSMonthCalendarUnit|NSWeekCalendarUnit|NSWeekdayCalendarUnit fromDate:[NSDate date]];


    [comps setWeekday:1]; 
    int monthComponent = comps.month;
   int yearComponent = comps.year;
   NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"yyyy MM dd"];

    NSDate *firstDateOfMonth = [dateFormatter dateFromString:[NSString stringWithFormat:@"%d %d 01",yearComponent,monthComponent]];
    firstDateOfMonth = [firstDateOfMonth dateByAddingTimeInterval:19800];
    NSLog(@"firstDateOfMonth-- %@ \r\n",firstDateOfMonth);
    NSLog(@"[NSDate date]-- %@",[[NSDate date] dateByAddingTimeInterval:19800]);
试试看

NSDate* date = [NSDate date];
NSDateFormatter* formatter = [[NSDateFormatter alloc] init];
NSTimeZone *destinationTimeZone = [NSTimeZone systemTimeZone];
formatter.timeZone = destinationTimeZone;
[formatter setDateStyle:NSDateFormatterLongStyle];
[formatter setDateFormat:@"MM/dd/yyyy hh:mma"];
NSString* dateString = [formatter stringFromDate:date];

正如桑尼所说,问题在于时区,以及对NSDate实际含义的误解

NSDate表示世界各地相同的时间点。如果我们在同一时刻调用[NSDate date],我们会得到相同的结果,并且NSLog会以相同的方式打印它。然而,如果我们都看手表,我们会看到不同的时间显示

你在印度。如果你正好在午夜看表,它显示下午12点。如果伦敦的一个人在同一时刻看手表,他将看到下午6:30。他们的表总是比你的表慢5个半小时。这就是NSLog显示的内容


例如,您正确计算了印度本月初的NSDate。这个月的开始是4月1日,也就是在印度时间一天开始的午夜。但此时此刻,伦敦仍然是3月31日18:30。这就是NSDate显示的内容。你总是得到正确的结果

不,这个问题是对NSDate的误解。