Get current time:
NSDate * senddate=[nsdate Date];
NSDateFormatter *dateformatter=[[nsdateformatter alloc] init];
[Dateformatter Setdateformat:@ "YYYYMMDD"];
NSString * Locationstring=[dateformatter stringfromdate:senddate];
NSLog (@ "locationstring:%@", locationstring);
[Dateformatter release];
Comparison of dates
1. Dates can be compared to determine size or equality, or you can determine the time interval between two dates. Two-day interval difference you can use the-timeintervalsincedate: method to calculate
- NSDate * now = [NSDate Date];
- NSDate * Anhourago = [now datebyaddingtimeinterval:-60*60];
- Nstimeinterval Timebetween = [now Timeintervalsincedate:anhourago];
- NSLog (@ "%f", Timebetween);
2. Date comparisons can also be obtained using the-timeintervalsincenow method and the current time interval
- NSDate * Anhourago = [NSDate datewithtimeintervalsincenow;-60*60];
- Nstimeinterval interval = [Anhourago timeintervalsincenow];
- NSLog (@ "%f", interval);
The 3.NSDate also provides-laterdate,-earlierdate, and compare methods to compare dates
- NSDate * now = [NSDate Date];
- NSDate * Anhourago = [now datebyaddingtimeinterval:-60*60];
- NSDate *RESULT1 = [now Laterdate:anhourago];
- NSDate * RESULT2 = [now Earlierdate:anhourago];
- Nscomparisonresult RESULT3 = [now Compare:anhourago];
Comparison of iOS development dates