NSDate and NSDateFormatter Use tips

Source: Internet
Author: User

1. Show Current Time

  NSDate *date = [nsdate date]; gets the current time

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

[Formatter setdateformat:@ "Yyyy-mm-dd a HH:mm:ss eeee"];

NSLog (@ "date =%@", [formatter stringfromdate:date]);

//output result is date = 2015-10-09 pm 20:34:05 Friday

   // Get the time of yesterday

nsdate *dateofyesterday = [nsdate datewithtimeinterval:- *3600 sincedate:d ate];

NSLog(@ "yesterday =%@", [Formatter stringfromdate:d ateofyesterday]);

// output yesterday = 2015-10-08 pm 20:55:28 Thursday

//2. Displays the number of seconds in the common time format

nsdate *dates = [nsdate dateWithTimeIntervalSince1970:+];

nsdateformatter *formatters = [[nsdateformatter alloc]init ];

   [formatters setdateformat:@ "Mm:ss"];

NSLog(@ "100s =%@", [formatters stringfromdate:d ates]);

// output is 100s = 01:40

3. customizing the regional language

Formatter. Locale = [nslocale localewithlocaleidentifier:@ "en_US"];//Celestial is ZH_CN

NSLog(@ "data =%@", [Formatter stringfromdate:d ate]);

// output data = 2015-10-09 PM 20:43:11 Friday

4. convert string to bit nsdate type

nsstring *datestring = @ "2015-07-16 am 11:52:49";

[Formatter settimezone: [nstimezone systemtimezone]];

[Formatter setdateformat:@ "Yyyy-m-dd aHH:mm:ss"];

nsdate *datefromstring =[formatter datefromstring:d atestring];

NSDate and NSDateFormatter Use tips

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.