zoukankan      html  css  js  c++  java
  • Hands on tutorial to NSDateComponents

    Hands on tutorial to NSDateComponents

    Link Address:http://chbeer.de/2010/07/hands-on-tutorial-to-nsdatecomponents/

    Calculating dates is quite a hard thing to do. You need to keep in mind that adding or subtracting(减) days (or months, hours, …) to a date can lead to changing months or even years: Getting the date of “two days before the first of January” leads to substract a year and a month from the current date, the result is the 29th of December a year before. You even need to keep leap years in mind.

    But there are tools in Cocoa that takes all the heavy stuff(材料、原料) off of calculating dates: the NSDate, NSCalendar and NSDateComponents classes. There are (at minimum) two things you can do using NSDateComponents: get components of a date and calculate new dates by changing components.

    Getting Date Components

    Getting date components of a given NSDate is very easy using NSDateComponents:

    -(int)month:(NSDate*)date

    {
      NSDateComponents *comps = [[NSCalendar currentCalendar] components:NSMonthCalendarUnit fromDate:date];
      return comps.month;
    }

    To access components of a given NSDate we are using NSCalendar(Calendar :日历) to get a group of components of it. Here we are interested in the month. So at first we are asking NSCalendar for the NSMonthCalendarUnit of our given date.

    After that we can access the specified units from the NSDateComponent. What’s important to know is that accessing units we didn’t specify on components:fromDate won’t give the correct values but more or less random values.

    You can also use NSDateComponents to get a date that only contains the values for given components. For example you can get a NSDate that contains the beginning of the day without hours, minutes, seconds, etc.:


    NSDate *date = [NSDate date];
    NSDateComponents *comps = [[NSCalendar currentCalendar] components:(NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit)
    fromDate:date];
    NSDate *beginningOfDay = [[NSCalendar currentCalendar] dateFromComponents:comps];

    Calculating New Dates

    To calculate new dates from a given date, you can configure a NSDateComponents instance as an offset to a date:

    -(NSDate *)previousMonth:(NSDate*)date

    {
      NSDateComponents *comps = [[NSDateComponents alloc] init];
      [comps setMonth:-1];
      NSCalendar *cal = [NSCalendar currentCalendar];
      NSDate *date = [cal dateByAddingComponents:comps toDate:date options:0];
      [comps release];
      return date;
    }

    At first we instantiate(vt:示例) a NSDateComponent and configure it with the offsets we want to add to the NSDate. In this example we want to calculate the date a month before today so we set the month to -1.

    After that we create a new NSDate by adding the components to the given date.

    Summary

    NSDateComponents provides some really great functionality(功能性) for creating dates with only requested components and to calculate dates as offsets from a given date. The class ensures that the calculated date is valid.

    THE END OF THIS BLOG !

  • 相关阅读:
    Jser 设计模式系列之面向对象
    jQuery 2.0.3 源码分析 回溯魔法 end()和pushStack()
    jQuery 2.0.3 源码分析 数据缓存
    jQuery 2.0.3 源码分析 Deferred(最细的实现剖析,带图)
    jQuery 2.0.3 源码分析 Deferred概念
    jQuery 2.0.3 源码分析 回调对象
    试试看 ? 离奇古怪的javascript题目
    jQuery 2.0.3 源码分析Sizzle引擎
    设计模式之美:Memento(备忘录)
    设计模式之美:Mediator(中介者)
  • 原文地址:https://www.cnblogs.com/xingchen/p/2238123.html
Copyright © 2011-2022 走看看