1016-09-homepage 22-date conversion, 1016-09-homepage 22-
The two nsdates can be compared.
It is easy to compare two dates with NSCalendar.
/**
1. This year
1> today
* Within 1: Just now
* 1 minute ~ Within 59 minutes: xx minutes ago
* More than 60 Minutes: xx hours ago
2> yesterday
* Yesterday xx: xx
3> others
* Xx-xx: xx
2. Not this year
1> xxxx-xx: xx
*/
-(NSString *) created_at
{
// _ Created_at = Thu Oct 16 17:06:25 + 0800 2014
// DateFormat = eee mmm dd HH: mm: ss Z yyyy
// NSString --> NSDate
NSDateFormatter * fmt = [[NSDateFormatter alloc] init];
// If debugging is performed on a real machine, you need to set locale to convert the time in Europe and America.
Fmt. locale = [[NSLocale alloc] initWithLocaleIdentifier: @ "en_US"];
// Set the date format (declare the meaning of each number and word in the string)
// E: day of the week
// M: Month
// D: The day of the month)
// H: 24 hours
// M: minute
// S: seconds
// Y: Year
Fmt. dateFormat = @ "eee mmm dd HH: mm: ss Z yyyy ";
// Weibo creation date
NSDate * createDate = [fmt dateFromString: _ created_at];
// Current time
NSDate * now = [NSDate date];
// Calendar Object (to compare the gap between two dates)
NSCalendar * calendar = [NSCalendar currentCalendar];
// Alendarunit enumeration indicates the difference value to be obtained
NSCalendarUnit unit = NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay | NSCalendarUnitHour | NSCalendarUnitMinute | NSCalendarUnitSecond;
// Calculate the difference between two dates
NSDateComponents * cmps = [calendar components: unit fromDate: createDate toDate: now options: 0];
HWLog (@ "% @", createDate, now, cmps );
Return _ created_at;
}