NSDate *now = [NSDate Date];
NSDateFormatter *fmt = [[NSDateFormatter alloc] init];
@ "Yyyy-mm-dd HH:mm:ss EEE Z" date and time zone difference Mon +0800
@ "yyyy mm month DD day hh" mm min ss sec "
Fmt.dateformat = @ "Yyyy-mm-dd HH:mm:ss";
NSString *nowstring = [FMT Stringfromdate:now];
NSLog (@ "%@", nowstring);
The format of the #warning string must be the same as the DateFormat format
@ "October 15, 2015 12:20 32 seconds"
NSString *s = @ "2013-1-15 12:20:32";
NSDate *lastdate = [FMT datefromstring:s];
NSLog (@ "%@", lastdate);
NSString *lasts = [FMT stringfromdate:lastdate];
NSLog (@ "%@", lasts);
Nscalendar *calendar = [Nscalendar Currentcalendar];
Nscalendarunit unit = Nscalendarunityear | Nscalendarunitmonth | Nscalendarunitday | Nscalendarunithour | Nscalendarunitminute | Nscalendarunitsecond;
Time difference Component
nsdatecomponents *components = [Calendar components:unit fromdate:lastdate todate:now options: Nscalendarwrapcomponents];
NSLog (@ "%ld%ld", Components.year, Components.month);
NSString *timespace;
if (Components.year > 0) {
Timespace = [NSString stringwithformat:@ "%ld%ld months%ld days ago", Components.year, Components.month, Components.day];
} else {
if (Components.month > 0) {
Timespace = [NSString stringwithformat:@ "%ld months ago", Components.month];
} else {
if (Components.day > 0) {
Timespace = [NSString stringwithformat:@ "%ld days Ago", Components.day];
}
}
}
NSLog (@ "%@", timespace);
How long is two days apart?