NSDate operations (comparison, creation, addition or subtraction of a certain period of time on the existing date), nsdatedate

Source: Internet
Author: User

NSDate operations (comparison, creation, addition or subtraction of a certain period of time on the existing date), nsdatedate

Current Creation Time

 

[Cpp]View plaincopyprint?
  1. NSDate * date = [NSDate date];

 

 

24 hours from now on

NSTimeInterval a_day = 24*60*60;

NSDate * tomorrow = [NSDate dateWithTimeIntervalSinceNow: a_day];

Create date based on 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 is1_todate: tomorrow];
  2. F (isEqual ){
  3. NSLog (@ "equal ");

 

// Obtain the earlier date

 

[Cpp]View plaincopyprint?
  1. NSDate * earlier_date = [tomorrow earlierDate: date];
  2. NSLog (@ "earlierDate = % @", earlierDate );

 

// Date later

 

[Cpp]View plaincopyprint?
  1. NSDate * laterDate = [tomorrow laterDate: date];
  2. NSLog (@ "laterDate = % @", laterDate );

 

// The number of seconds between two dates

 

[Cpp]View plaincopyprint?
  1. NSTimeInterval secondsInterval = [date timeIntervalSinceDate: tomorrow];
  2. NSLog (@ "secondsInterval = % lf", secondsInterval );

 

 

// Create a date 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];

 

// Obtain the date from the 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 implements date output
  7. NSDateFormatter * formatter = [[NSDateFormatteralloc] init];
  8. [Formatter setDateStyle: NSDateFormatterFullStyle]; // the machine code is output directly.
  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 );

 

// Obtain the 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. }

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.