003-Date and Time (NSDate)

Source: Internet
Author: User

#import <foundation/foundation.h>int Main (int argc, const char * argv[]) {@autoreleasepool {//Get represents the current date, when        Between the nsdate nsdate* date1 = [NSDate Date];                NSLog (@ "%@", date1);        Gets the date starting from the current time, after one day nsdate* date2 = [[NSDate alloc]initwithtimeintervalsincenow:3600*24];                        NSLog (@ "%@", date2);        Gets the date starting from the current time, 3 days ago nsdate* date3 = [[NSDate alloc]initwithtimeintervalsincenow: -3600*24*3];                NSLog (@ "%@", date3);        Obtained starting from January 1, 1970, 20 years after the date nsdate* date4 = [[NSDate alloc]initwithtimeintervalsince1970:3600*24*366*20];                        NSLog (@ "%@", date4);        Gets the current locale of the system nslocale* CN = [Nslocale Currentlocale];                Gets the NSDate string NSLog (@ "%@", [date1 DESCRIPTIONWITHLOCALE:CN]) corresponding to the current locale;        Get an earlier date between two dates nsdate* earlier = [Date1 Earlierdate:date2];                NSLog (@ "earlier time:%@", earlier); Get a later time between two dates nsdate* later = [daTe1 Laterdate:date2];                NSLog (@ "later time:%@", later); Compare two dates, compare: Method returns Nscomparisonresult enumeration value//The enumeration type contains nsorderdascending,nsordersame,nsordereddescending three values//points Do not represent call compare: the date is the same as before the comparison date, and then switch ([Date1 compare:date3]) {Case Nsorderedascending:nslog ( @ "Date1 in front of Date3");            Break Case Nsorderedsame:nslog (@ "date1 and date3 date equal");            Break        Case Nsordereddescending:nslog (@ "Date1 is located after Date3");                }//Get the time difference between two times NSLog (@ "date1 and date3 between%g seconds", [Date1 timeintervalsincedate:date3]);    Gets the time difference from the current NSLog (@ "Date2 with the current difference%g seconds", [Date2 Timeintervalsincenow]); } return 0;}

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

003-Date and Time (NSDate)

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.