How does iOS convert a date string to NSDate?

Source: Internet
Author: User

You can get the date string "Wed May 2 22:27:08 + 0800 2012" and convert it to NSDate for date calculation. You can set the corresponding NSDateFormatter according to the online information, then convert it to the corresponding NSDate. The corresponding code snippet is as follows:

NSString * dateStr = @ "Wed May 2 22:27:08 + 0800 2012 ";

NSDateFormatter * formater = [[NSDateFormatter alloc] init];

[Formater setDateFormat: @ "eee mmm d HH: mm: ss zzzz yyyy"];
NSDate * date = [formater dateFromString: dateStr];
NSLog (@ "% @", date );

// NSDate * now = [NSDate now];
Double inter = fabs ([date timeIntervalSinceNow]);
If (inter <60)
NSLog (@ "1 mins ago! ");
Else if (inter <60*60)
NSLog (@ "1 hours ago! ");
Else if (inter <60*60*24)
NSLog (@ "1 days ago! ");

NSLog (@ "interval is % f min", inter/60 );


From the keyken Column

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.