-2

I need the beginning and ending of the current day in NSDate format since i have an nspredicate to get the datas between this range. so i wrote down a function to find beginning of current day and this is it

NSDate *now = [NSDate date];
NSCalendar *calendar = [NSCalendar currentCalendar];
NSDateComponents *components = [calendar components:(NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit) fromDate:now];
[components setHour:0];
[components setMinute:0];
[components setSecond:0];
NSDate *start = [calendar dateFromComponents:components];
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
NSString *strFromDate = [formatter stringFromDate:start];

when i try to nslog strFromDate it is showing the exact starting of current day like this 2013-05-05 00:00:00 +0000 but i need it in nsdate format so when i try to convert it into the nsdate like this

NSDateFormatter * dateFormat = [[NSDateFormatter alloc]init];
[dateFormat setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
NSDate *dateStr = [[NSDate alloc]init];
dateStr = [dateFormat dateFromString:strFromDate];

and when i nslog it the result becomes 2013-05-05 18:30:00 +0000. i have tried a lot of codes and when i tried to print it in nsstring format i got the desired result do not know what goes wrong with nsdate. If anyone could guide me. Thanks in advance

4

1 回答 1

2

当我 nslog 它时,结果变为2013-05-05 18:30:00 +0000

仔细阅读该结果。最后+0000的意思很有意义。这意味着格林威治标准时间(伦敦时间)。所以这是说时间是18:30:00在伦敦。但是假设你在印度。那是5:30晚些时候,也就是午夜。这正是你想要的。

于 2013-05-05T14:58:26.677 回答