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

时间:2013-04-02 03:53:33

标签: iphone objective-c nsdate nsdateformatter nsdatecomponents

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

我目前在印度的时间是2013-04-02 09:10:__ AM 但输出是2013-04-02 03:54:51 +0000

2013- 4月 - 2月:上午9:10

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

即4月1日至4月2日上午9点10分当前时间2013-04-02 09:10:__ AM

但显示

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

当前时间为2013- 04-02 03:54 :51 +0000 但实际时间是09:10:__ AM

我按如下方式计算当月的第一个日期

// 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甚至认为 i got wrong results for Current date and firstDateOfMonth

提前致谢

4 个答案:

答案 0 :(得分:0)

问题时区。 默认情况下,NSDateFormatter设置为您的本地时区。这意味着,如果您的时区是+5:30,则在1970-03-18 00:00:00 +0530之间给出日期“1970/18/3”。但是,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]);

答案 1 :(得分:0)

试试吧......

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];

答案 2 :(得分:0)

正如Sunny所说,问题是时区,加上对NSDate实际意味着什么的误解。

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

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

例如,您在印度正确计算了本月初的NSDate。本月初是在印度时间4月1日,当天午夜的午夜。但在那个时刻,它仍然是伦敦3月31日18:30。这就是NSDate所展示的。你总是得到正确的结果。

答案 3 :(得分:-1)

@IOS dev,

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

xcode 4.2 location simulator and Time Zone 另请查看[NSTimeZone localTimeZone]

见以下链接:

NSTimeZone: what is the difference between localTimeZone and systemTimeZone?

相关问题