1016-09-Home 22-date Conversion

Source: Internet
Author: User
Tags american time dateformat what date

Two nsdate can be compared between the two.

It's easy to compare two dates with Nscalendar.


/**
1. This year
1> today
* 1: Just
* 1 min ~59: xx minutes ago
* More than 60 minutes: xx hours ago

2> yesterday
* Yesterday xx:xx

3> Other
* Xx-xx xx:xx

2. Not this year
1> xxxx-xx-xx 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 it is a real-machine debug, convert this European and American time, you need to set locale
Fmt.locale = [[Nslocale alloc] initwithlocaleidentifier:@ "en_US"];

Format the date (the meaning of each number and word inside the declaration string)

E: Day of the week
M: Month
D: What date (the day of the month)
H: 24-hour hour
M: minutes
S: sec
Y: Year
Fmt.dateformat = @ "EEE MMM dd HH:mm:ss Z yyyy";

Date of creation of Weibo
NSDate *createdate = [FMT datefromstring:_created_at];
Current time
NSDate *now = [NSDate Date];
Calendar object (easy to compare the gap between two dates)
Nscalendar *calendar = [Nscalendar Currentcalendar];
Nscalendarunit enumeration represents what difference you want to get
Nscalendarunit unit = Nscalendarunityear | Nscalendarunitmonth | Nscalendarunitday | Nscalendarunithour | Nscalendarunitminute | Nscalendarunitsecond;
Calculates the difference between two dates
nsdatecomponents *cmps = [Calendar components:unit fromdate:createdate Todate:now options:0];

Hwlog (@ "%@%@%@", CreateDate, now, cmps);

return _created_at;
}

1016-09-Home 22-date Conversion

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.