2013-04-02 41 views
0

其实在我simulater时间显示近5个小时30分钟问题显示5小时30分钟过去时

我目前在印度的时间是2013年4月2日09:10 :__ AM但产量是2013年4月2日3点54分51秒 +0000

2013-四月 - 2日:上午9:10

我需要得到两个日期之间1.首先一些成果当前日期至2.当前日期

即从4月1日至4月2日上午9.10 T0当前时间2013年4月2日09:10 :__ AM

但dispalys

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

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

我COM普特月份的第一次约会如下

// 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

在此先感谢

回答

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

的问题是时区,晴天说,加什么的NSDate,其实就是一种误解。

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

你在印度。如果您在午夜时分观看手表,则会显示12点。如果伦敦的一个人在同一时间看他们的手表,那个人将在下午6:30看到他们的手表。他们的手表总是在你身后5个半小时。这就是NSLog显示的内容。

例如,您正确计算了印度月初的NSDate。本月初是4月1日,即在印度时间的午夜。但在那个时候,它仍然是三月三十一日在伦敦,在18:30。这就是NSDate显示的内容。你总是得到正确的结果。