NSDate only gets the implementation of the date and year--that is, as long as the month and day, do not spare hours minutes of seconds

Source: Internet
Author: User

(Only personal, if there is a difference, or not high efficiency also please point out, thank you)

Recently in writing a back word app, according to Ebbinghaus curve to review the enhanced memory, need to calculate two review of the difference of days, just need to get a (not include the day time) date

For example, we get the current time using [NSDate Date], and we get the following data:

2015-03-13 14:08:55 +0000

Obviously, I just want to 2015-03-13, it is more than 14:08:55 (the specific time of the day), and then turn into nstimeinterval when the calculation of two times the time difference may not meet my criteria for review requirements (that is, not 1 days apart 24*60*60 s or 2 days. )

So what are the methods for post-processing to obtain only the date and month-day years? When I write the app, I think there are two simple ways to deal with it, a little comparison:

Fayi (not recommended): NSDateFormatter

1-(NSDate *) Extractdate: (NSDate *) Date {2     3NSDateFormatter *fmt =[[NSDateFormatter alloc] init];4Fmt.locale = [[Nslocale alloc] Initwithlocaleidentifier:@"zh_ch"];5Fmt.dateformat =@"yyyy. Mm.dd";6     7NSString *temp =[FMT stringfromdate:date];8     return[FMT datefromstring:temp];9}

As shown above, the second half is omitted by NSDateFormatter.

Law II (recommended): Nstimeinterval calculation

1-(NSDate *) Extractdate: (NSDate *) Date {2     //get seconds since 19703Nstimeinterval interval =[Date timeIntervalSince1970];4     intDayseconds = -* -* -;5     //Calculate integer type of Days6Nsinteger Alldays = interval/dayseconds;7     8     return[NSDate Datewithtimeintervalsince1970:alldays *Dayseconds];9}

This method is to obtain the current time (including the time) and 1970 (1970.1.1) of the difference (in units of second), and then total seconds divided by the seconds of the day, you can calculate the number of days to omit the extra specific time

Then use time Profiler for a small test, to speak with the data, to see the two methods of efficiency which high (the same 1000 data call this method in turn)

Law One:

64ms, Pit Daddy, at least I can feel a moment of nsdateformatter when testing the app, which also shows that the datefromstring efficiency is very low

Law II:

No more extractdate This method takes up so much time, the main thread total is only 83ms Ah!!! It's obvious that law two wins.

NSDate only gets the implementation of the date and year--that is, as long as the month and day, do not spare hours minutes of seconds

Related Article

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.