NSDateComponents issue - incorrect day

后端 未结 3 959
我在风中等你
我在风中等你 2021-01-19 14:34

I have a NSDateComponents problem. I have two NSDates that I am trying to compare by checking if their year, month and day match. This I am doing by converting the NSDate va

相关标签:
3条回答
  • 2021-01-19 15:17

    NSDate has an isEqualToDate: function. so what you are trying to do can be done by:

    [datea isEqualToDate:dateb];
    

    there is also a Compare: function available, that returns the ordering of the dates.

    edit: I'm now aware this doesn't answer your question about why the days return different values when they are set the same. Sorry! this still may help make your code a bit cleaner.

    0 讨论(0)
  • 2021-01-19 15:31

    I think the issue is the following: Your dates are set in GMT time zone (+0000) If you are in the US, for example at GTM-6, then by the -currentCalendar the first date will be 6pm on Aug 3rd while second date will be 8:49am on Aug 4th.

    You should force your calendar to have the UTC (GMT) timezone, or put the dates in your time zone, depending what is correct for your application.

    0 讨论(0)
  • 2021-01-19 15:37

    It looks like you have a time zone issue. Though your dates are set at time zone +0000, your [NSCalendar calendar] call likely returns you a calendar for your local time zone. Given the adjustment to local time, the orderDate is likely in the next day. Manually set your calendars to time zone +0000.

    0 讨论(0)
提交回复
热议问题