简体   繁体   English

从UIDatePicker中选择的日期与保存的日期不同

[英]Date selected from UIDatePicker is different from the date being saved

This is how I setup my datePicker 这就是我设置datePicker的方法

self.datePicker = [[UIDatePicker alloc] init];
self.datePicker.timeZone = [NSTimeZone localTimeZone];

This is how I save the date that I selected 这就是我保存所选日期的方式

NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateStyle:NSDateFormatterMediumStyle];
[formatter setTimeZone:[NSTimeZone localTimeZone]];
dateToSave = [formatter dateFromString:self.dateTextField.text];
NSLog(@"date saved = %@", dateToSave);

If I select Nov 18 2013 from the date picker, the NSLog shows 如果我从日期选择器中选择2013年11月18日,NSLog会显示

date saved = 2013-11-17 16:00:00 +0000

However, somewhere in my code, I need to get the difference in days between today's date and the date that I selected in the datepicker. 但是,在我的代码中的某个地方,我需要在今天的日期和我在datepicker中选择的日期之间的天数中获得差异。

NSDateComponents *dateComponents = [[NSCalendar currentCalendar] components:NSDayCalendarUnit fromDate:[NSDate date] toDate:dateSaved options:0];
NSLog(@"number of days => %i", [dateComponents day]);

Today is Nov 10. The date I saved is Nov 18. But the number of days difference is 7, instead of 8. 今天是11月10日。我保存的日期是11月18日。但天数差异是7,而不是8。

Your time zone is -8. 你的时区是-8。 2013-11-17 16:00:00 +0000 equals to 2013-11-18 00:00:00 -0800 . 2013-11-17 16:00:00 +0000等于2013-11-18 00:00:00 -0800

Use [NSTimeZone timeZoneForSecondsFromGMT:0] instead of [NSTimeZone localTimeZone] 使用[NSTimeZone timeZoneForSecondsFromGMT:0]代替[NSTimeZone localTimeZone]

(This answer refers to the updated question about calculating the number of days between two dates.) (此答案是指有关计算两个日期之间天数的更新问题。)

The problem is that [NSDate date] is the current date+time, not the start of the current day. 问题是[NSDate date]是当前日期+时间,而不是当天的开始日期。 For example, if 例如,如果

 [NSDate date] = "2013-11-10 10:00:00"
 dateSaved     = "2013-11-18 00:00:00"    (both in your *local* timezone)

then the difference between these two dates is "7 days and 14 hours". 那么这两个日期之间的差异是“7天14小时”。 Therefore you get 7 as the number of days. 因此,您获得7天的天数。

So you have to calculate the start of the current day first: 所以你必须先计算当天的开始时间:

NSDate *startOfDay;
[[NSCalendar currentCalendar] rangeOfUnit:NSDayCalendarUnit
                                startDate:&startOfDay
                                 interval:NULL
                                  forDate:[NSDate date]];

and then use it in the calculation of the difference: 然后用它来计算差异:

NSDateComponents *dateComponents = [[NSCalendar currentCalendar] components:NSDayCalendarUnit
                                           fromDate:startOfDay
                                             toDate:dateSaved
                                            options:0];
NSDateFormatter *date_form=[[NSDateFormatter alloc]init];

[date_form setDateFormat:@"dd/MM/yyyy"];

NSDate *seletected_date = [datepicker date];

NSString *dateToSave=[[NSString alloc] initWithFormat:@"%@",[date_form stringFromDate:seletected_date]]; 

NSLog(@"date saved = %@", dateToSave);

Remove localtimezone 删除localtimezone

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM