1016-09-homepage 22-date conversion, 1016-09-homepage 22-

Source: Internet
Author: User

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;
}

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.