Common iOS time methods

Source: Internet
Author: User

Common iOS time methods

// Date before the current day

NSDate * today = [NSDate date];

 

// Time Zone

NSTimeZone * zone = [NSTimeZone systemTimeZone];

 

// Set the interval

_ Interval = [zone secondsFromGMTForDate: today];

 

// Today + 8 hours (China Shanghai time zone)

Today = [today dateByAddingTimeInterval: _ interval];

 

// Obtain tomorrow's date

NSDate * tomorrowDate = [[NSDate alloc] initWithTimeInterval: 24*60*60 sinceDate: [NSDate date];

 

// Calendar

NSCalendar * calendar = [NSCalendar currentCalendar];

 

// Information such as time, year, month, and day can be separated from comps.

 

NSDateComponents * comps = [calendar components :( NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit | NSWeekCalendarUnit | NSWeekdayCalendarUnit | bytes | NSHourCalendarUnit) fromDate: tomorrowDate];

 

// Obtain the current day of the week

NSInteger weekday = [comps weekday];

 

// Convert the NSString type to NSDate

 

NSDate * serverDate = [dateFomatter dateFromString: serverTimeStr];

NSDate * createDate = [dateFomatter dateFromString: createTimeStr];

 

// Calculate the interval between two dates

NSTimeInterval betweenTime = [serverDate timeIntervalSinceDate: createDate];


// Separate the year, month, day, hour, minute, and second in the standard format

 

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

[DateFomatter setDateFormat: @ "yyyy-MM-dd HH: mm: ss"];

 

// Replace string with formatter

NSDate * stringDate = [dateFomatter dateFromString: string];

 

// Formatter is month and day

[DateFomatter setDateFormat: @ "MM-dd"];

 

// Extract the string month and day

NSString * str1 = [dateFomatter stringFromDate: stringDate];


 


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.