IOS Calendar class (Nscalendar)

Source: Internet
Author: User

Time-based operations are common in development, but sometimes we need to get to the time of year, or the time of week. It is not possible to calculate by the number of seconds. That involves another calendar class (Nscalendar).
Let's take a quick look at nsdate.

let date = NSDate()let formatter = NSDateFormatter() formatter.dateFormat = "yyyy-MM-dd HH-mm-ss"formatter.stringFromDate(date)// 延迟多少秒为正数 前多少秒为负数let date2 = date.dateByAddingTimeInterval(500) formatter.stringFromDate(date2)// 返回两个时间较早的时间let earlierDate = date.earlierDate(date2)// 返回较晚的时间let laterDate = date.laterDate(date2)

By the above we found the difference between two days we can postpone 3600 *24, one months, sometimes 30 days, sometimes 31 days. That's hard to calculate.
Here's a look at how to use Nscalendar.

The calendar gets an exception when the system uses Currentcalendar after 9.x. Use the new system API after 8.0. Let calendar = Nscalendar.currentcalendar ()//is a string that needs to be passed, because the calendar can have many kinds of calendars that are chosen here is the Gregorian calendar = Nscalendar ( Calendaridentifier:nscalendaridentifiergregorian)  //8.0 New method after  /* calendar type @available (IOS, introduced=2.0, deprecated=8.0, message= "use Nscalendaridentifiergregorian instead") publicly let nsgregoriancalendar:string//Gregorian @avail Able (IOS, introduced=2.0, deprecated=8.0, message= "use Nscalendaridentifierbuddhist instead") nsbuddhistcalendar:string//Buddha calendar @available (IOS, introduced=2.0, deprecated=8.0, message= "use Nscalendaridentifierchinese instead ") public let nschinesecalendar:string//Lunar @available (IOS, introduced=2.0, Depreca ted=8.0, message= "use Nscalendaridentifierhebrew instead") public let nshebrewcalendar:string//Hebrew calendar @available (IOS, introduced=2.0, deprecated=8.0, message= "use Nscalendaridentifierislamic instead") publicly let Nsislamiccalendar: String//Islamic calendar @available (IOS, Introduced=2.0, deprecated=8.0, message= "use Nscalendaridentifierislamiccivil instead") publicly let Nsislamiccivilcalendar: String//Islamic Civil calendar @available (IOS, introduced=2.0, deprecated=8.0, message= "use Nscalendaridentifierjapanese instead") p Ublic let nsjapanesecalendar:string//Japan calendar @available (IOS, introduced=4.0, deprecated=8.0, message= "Use Nscalendariden Tifierrepublicofchina instead ") public let nsrepublicofchinacalendar:string//China calendar @available (IOS, introduced=4.0, DEP recated=8.0, message= "use Nscalendaridentifierpersian instead") public let nspersiancalendar:string//Persian calendar @available ( IOS, introduced=4.0, deprecated=8.0, message= "use Nscalendaridentifierindian instead") publicly let Nsindiancalendar: String//India Calendar @available (IOS, introduced=4.0, deprecated=8.0, message= "use NSCalendarIdentifierISO8601 instead")  public let nsiso8601calendar:string//International Organization for Standardization Calendar */

With the Calendar class we are easy to operate, here are two ways to
Method One:

// 日期比较的类let adcomps = NSDateComponents() adcomps.year = 1adcomps.month = -1adcomps.day = 1// 追加时间可以一次性设置多值let date5 =  calendar?.dateByAddingComponents(adcomps, toDate: date, options: .WrapComponents) formatter.stringFromDate(date5!)

Attention:

    • . Wrapcomponents cannot be rounded up. This means that the month of January does not add 1, but the day loop. What does that mean?
      For example, today is 2016-07-07. Just set Adcomps.day =. Then the result is 2016-07-31;
      So if you set Adcomps.day = , the result is not 2016-08-01, but 2016-07-01. That is, to do day in the loop, the effect is the same for month setting.
      If you want to carry with other optional values
      The value of setting option directly in OC is 0;

Method Two:

let date6 = calendar?.dateByAddingUnit(.Year, value: 1, toDate: date, options: .WrapComponents)

IOS Calendar class (Nscalendar)

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.