IOS SDK Nscalendar & nsdate? Components

Source: Internet
Author: User
Tags dateformat time zones

Original blog, reproduced please indicate the source
Blog.csdn.net/hello_hwc
Happy drink Follow my iOS SDK detailed column
Http://blog.csdn.net/column/details/huangwenchen-ios-sdk.html

Preface: Nscalendar and NSDate? Components are two classes that are important for the iOS time-related API. It's just been used recently, and it's sorted out here.

Concept
    • Nscalendar, as the name implies, is a calendar that encapsulates how the system organizes time by month and day, how the time is organized and the region, and the time zone is of great relevance.
    • NSDate? The components are just a container that contains information such as month and day.
    • NSDate is an absolute time that can be converted to a relative 1970s of jet lag.
Example

Split a time
Oc

 nsdate  * date = [ Date]; //Current time  nscalendar  * calendar = [nscalendar  Currentcalendar]; //the current user's calendar  nsdatecomponents * components = [Calendar Components:nscalendarunitminute | Nscalendarunitmonth | Nscalendarunithour | Nscalendarunitday Fromdate:date]; nslog  (@, (long ) Components.month , (long ) components .day , (long ) components .hour , (long ) components .minute ); 

Swift

   let date = NSDate()   let calendar = NSCalendar.currentCalendar()   let components = calendar.components(NSCalendarUnit.CalendarUnitMonth | NSCalendarUnit.CalendarUnitDay | NSCalendarUnit.CalendarUnitHour | NSCalendarUnit.CalendarUnitMinute, fromDate:date)   NSLog("%ld月%ld日%ld时%ld分" ,components.month, components.day, components.hour, components.minute)

relative ordinal
For example, view today is the first week of the year

  NSCalendar * calendar = [NSCalendar currentCalendar];    NSDate * currentDate = [NSDate date];    NSInteger week = [calendar ordinalityOfUnit:NSCalendarUnitWeekday inUnit:NSCalendarUnitYear forDate:currentDate];    NSLog(@"今天是今年的第%ld周",week);

returns nsdate based on split time
Oc

nsdatecomponents * components = [[Nsdatecomponents alloc] init]; Components. Year= -; Components.Month=7; Components. Day= One; Components.Hour=Ten; Components.minute= -; Components.Second= -;    Nscalendar * Calendar = [Nscalendar Currentcalendar]; NSDate *Date= [Calendar datefromcomponents:components];    NSDateFormatter * formatter = [[NSDateFormatter alloc] init]; Formatter.dateformat = @"yyyy mm month DD day hh" mm min ss sec "; NSString * str = [Formatter stringfromdate:Date]; NSLog (@"%@", str);

Swift

   Letcomponents = Nsdatecomponents (). Year= -Components.Month=7Components. Day= OneComponents.Hour=TenComponents.minute= -Components.Second= -         LetCalendar = Nscalendar.currentcalendar () Let Date= calendar.datefromcomponents (components) LetFormatter = NSDateFormatter (); Formatter.dateformat ="yyyy mm month DD day hh" mm min ss sec "; Letstr = formatter.stringfromdate (Date!); println (str)

Relative time

Now 11 days, 10 hours.
Oc

nsdatecomponents * components = [[Nsdatecomponents alloc] init]; Components. Day= One; Components. Hour=Ten;Nscalendar* Calendar = [NscalendarCurrentcalendar];NSDate* currentdate = [NSDateDate];NSDate* Nextdata = [Calendar datebyaddingcomponents:components todate:currentdate options:nscalendarmatchstrictly];    NSDateFormatter * formatter = [[NSDateFormatter alloc] init]; Formatter. DateFormat= @"yyyy mm month DD day hh" mm min ss sec ";NSString* str = [formatter stringfromdate:nextdata];NSLog(@"%@", str);

Swift

let1110;let calendar = NSCalendar.currentCalendar();let currentDate = NSDate()let nextDate = calendar.dateByAddingComponents(components, toDate: currentDate, options:NSCalendarOptions.MatchStrictly)let"yyyy年MM月dd日hh时mm分ss秒";let  str = formatter.stringFromDate(nextDate!)NSLog("%@",str);

How many days are there this month?

NSCalendar * calendar = [NSCalendar currentCalendar];    NSDate * currentDate = [NSDate date];    NSRange range = [calendar rangeOfUnit:NSCalendarUnitDay inUnit:NSCalendarUnitMonth forDate:currentDate];
Other

Create Nscalendar in different regions

initWithCalendarIdentifier:

One of the effective identifer

NSString*ConstNsgregoriancalendar;NSString*ConstNsbuddhistcalendar;NSString*ConstNschinesecalendar;NSString*ConstNshebrewcalendar;NSString*ConstNsislamiccalendar;NSString*ConstNsislamiccivilcalendar;NSString*ConstNsjapanesecalendar;NSString*ConstNsrepublicofchinacalendar;NSString*ConstNspersiancalendar;NSString*ConstNsindiancalendar;NSString*ConstNsiso8601calendar;

Start date of the week

– firstWeekday

Time Zones and regions

localetimeZone

All the Nscalendarunit

   NSEraCalendarUnit = kCFCalendarUnitEra,   NSYearCalendarUnit = kCFCalendarUnitYear,   NSMonthCalendarUnit = kCFCalendarUnitMonth,   NSDayCalendarUnit = kCFCalendarUnitDay,   NSHourCalendarUnit = kCFCalendarUnitHour,   NSMinuteCalendarUnit = kCFCalendarUnitMinute,   NSSecondCalendarUnit = kCFCalendarUnitSecond,   NSWeekCalendarUnit = kCFCalendarUnitWeek,   NSWeekdayCalendarUnit = kCFCalendarUnitWeekday,   NSWeekdayOrdinalCalendarUnit = kCFCalendarUnitWeekdayOrdinal,   NSQuarterCalendarUnit = kCFCalendarUnitQuarter,   NSWeekOfMonthCalendarUnit = kCFCalendarUnitWeekOfMonth,   NSWeekOfYearCalendarUnit = kCFCalendarUnitWeekOfYear,   NSYearForWeekOfYearCalendarUnit = kCFCalendarUnitYearForWeekOfYear    NSCalendarCalendarUnit = (1 << 20),   NSTimeZoneCalendarUnit = (1 << 21),

Copyright NOTICE: This article is for bloggers original article, if you need to reprint please indicate the source

IOS SDK Nscalendar & nsdate? Components

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.