NSDate Some operations (compare, create, add or subtract a certain amount of time on existing date, etc.)

Source: Internet
Author: User

Create Current time

[CPP]View Plaincopyprint?
    1. NSDate *date = [NSDate Date];

24 hours from now.

Nstimeinterval a_day = 24*60*60;

NSDate *tomorrow = [NSDate datewithtimeintervalsincenow:a_day];

Create a date based on an existing date

[CPP]View Plaincopyprint?
    1. Nstimeinterval a_day = 24*60*60;
    2. NSDate *date = [NSDate Date];
    3. NSDate *tomorrow = [date addtimeinterval:date];

Date comparison

[CPP]View Plaincopyprint?
    1. BOOL isequal = [date Isequaltodate:tomorrow];
    2. F (isequal) {
    3. NSLog (@"equal");

Get an earlier date

[CPP]View Plaincopyprint?
    1. NSDate *earlier_date = [Tomorrow earlierdate:date];
    2. NSLog (@"earlierdate =%@", earlierdate);

Later date

[CPP]View Plaincopyprint?
    1. NSDate *laterdate = [Tomorrow laterdate:date];
    2. NSLog (@"laterdate =%@", laterdate);

Number of seconds between two dates

[CPP]View Plaincopyprint?
    1. Nstimeinterval secondsinterval= [Date Timeintervalsincedate:tomorrow];
    2. NSLog (@"secondsinterval=%lf", secondsinterval);

To create a date by using the Nscalendar class

[CPP]View Plaincopyprint?
    1. Nsdatecomponents *comps = [[Nsdatecomponentsalloc]init];
    2. [Comps setmonth:01];
    3. [Comps setday:31];
    4. [Comps setyear:2013];
    5. Nscalendar *calendar = [[Nscalendaralloc]initwithcalendaridentifier:nsgregoriancalendar];
    6. NSDate *date = [Calendar Datefromcomponents:comps];

Get a date from an existing date

[CPP]View Plaincopyprint?
  1. unsigned units = nsmonthcalendarunit| Nsdaycalendarunit| Nsyearcalendarunit;
  2. nsdatecomponents *_comps = [Calendar components:units fromdate:date];
  3. Nsinteger month = [_comps month];
  4. Nsinteger year = [_comps year];
  5. Nsinteger day = [_comps day];
  6. NSDateFormatter output of the implementation date
  7. NSDateFormatter *formatter = [[Nsdateformatteralloc]init];
  8. [Formatter Setdatestyle:nsdateformatterfullstyle]; //Direct output is machine code
  9. or manually set the style [formatter setdateformat:@ "YYYY-MM-DD"];
  10. NSString *datestring = [Formatter stringfromdate:date];
  11. NSLog (@"datestring =%@", datestring);
  12. NSLog (@"formater =%@", formatter);

Get date Format Object

[CPP]View Plaincopyprint?
      1. -(NSDateFormatter *) Getdateformatter {
      2. if (dateformatter = = nil) {
      3. Dateformatter = [[NSDateFormatter alloc] init];
      4. [Dateformatter Setdatestyle:nsdateformattermediumstyle];
      5. [Dateformatter Settimestyle:nsdateformatternostyle];
      6. }
      7. return dateformatter;
      8. }

NSDate Some operations (compare, create, add or subtract a certain amount of time on existing date, etc.)

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.