IOS Date Related Summary

Source: Internet
Author: User

(NSString *) = = Thu OCT 16 17:06:25 +0800 2014

//DateFormat = EEE MMM dd HH:mm:ss Z yyyy

nsdateformatter *fmt = [[nsdateformatteralloc] 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"];

Fmt.locale = [[Nslocale alloc] initwithlocaleidentifier:@ "ZH_CN"];

//E: Day of the week

//M: Month

//d: What date ( the day of the month )

// hour of h:24 hours

//m: min

//s: sec

//Z: time zone

//y: Year

// Set Date format ( the meaning of each number and word inside the declaration string )

FMT. DateFormat = @ "EEE MMM dd HH:mm:ss Z yyyy" ;

nsdate *createdate = [FMT datefromstring:(NSString *)]; // turn (NSString *) into a Time object

//Fmt.dateformat = @ "Yyyy-mm-dd HH:mm:ss";// This format

//(NSString *) = [FMT stringfromdate:createdate];// Convert the time object to a string of this format

// Current time

nsdate *now = [nsdatedate];

// Calendar Object ( easy to compare the gap between two dates )

nscalendar *calendar = [nscalendarcurrentcalendar];

    /*

Nscalendarunitera = Kcfcalendarunitera,

Nscalendarunityear = Kcfcalendarunityear,

Nscalendarunitmonth = Kcfcalendarunitmonth,

Nscalendarunitday = Kcfcalendarunitday,

Nscalendarunithour = Kcfcalendarunithour,

Nscalendarunitminute = Kcfcalendarunitminute,

Nscalendarunitsecond = Kcfcalendarunitsecond,

Nscalendarunitweekday = Kcfcalendarunitweekday,

Nscalendarunitweekdayordinal = Kcfcalendarunitweekdayordinal,

*/

the //Nscalendarcalendarunit enumeration represents what difference you want to get

nscalendarunit unit = kcfcalendarunityear | Kcfcalendarunitmonth | Kcfcalendarunitday | Kcfcalendarunithour | Kcfcalendarunitminute | Kcfcalendarunitsecond ;

// calculate a time difference between two dates

nsdatecomponents *cmps = [Calendar Components: unit FromDate: createdate toDate : Now options: 0];

NSLog(@ "%@---%@---%@", CreateDate, now, cmps);

IOS Date Related Summary

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.