Date is only accurate to the time portion of the day ellipsis time

Source: Internet
Author: User

Personal feeling, more concise than the way of nsdatecomponents. The simple test is under, no problem. If you have any questions to test, please give us feedback.


TimeIntervalSince1970 is based on GMT time and needs to be corrected in time zone.

nstimezone* zone = [Nstimezone systemtimezone];

Nstimeinterval interval = olddate.timeintervalsince1970;

Nstimeinterval newinterval = Interval-fmod (interval+zone.secondsfromgmt,60*60*24);

The remainder of the interval minus interval divided by the number of seconds in a day.

Nstimeinterval is actually a double type, and% can only be used with the int type, so you need to use the FMOD function.

nsdate* newdate = [NSDate datewithtimeintervalsince1970:newinterval];


In a nsdatecomponents way:

unsigned int flags = Nsyearcalendarunit | Nsmonthcalendarunit | Nsdaycalendarunit;
nscalendar* calendar = [Nscalendar Currentcalendar];
nsdatecomponents* components = [Calendar components:flags fromdate:date];
nsdate* dateonly = [Calendar datefromcomponents:components];

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.