日期时间概述

来源:互联网 发布:vb与三菱plc通讯 编辑:程序博客网 时间:2024/06/05 00:52

About Dates and Times

Date and time objects allow you to store references to particularinstances in time.

日期和时间对象允许我们对 特定实例的引用进行存储。

You can use date and time objects to perform calculations andcomparisons that account for the corner cases of date and time calculations.

您可以使用日期和时间对象进行日期和时间的计算和比较(corner cases、小概率事件)

At a Glance

 

There are three main classes used for working with dates and times.

有三个主要的类用于完成日期时间相关的工作

 

NSDate allows you to represent an absolute point in time.

NSDate类允许我们表示一个绝对时间点。

 

NSCalendar allows you to represent a particular calendar, such as aGregorian or Hebrew calendar.

NSCalendar类允许我们表示特定的日历,如公历或希伯来日历。

It provides the interface for most date-based calculations and allowsyou to convert between NSDate objects and NSDateComponents objects.

它提供了大部分 基于日期的计算的接口,允许我们在NSDate的对象和NSDateComponents对象之间进行转换。

 

NSDateComponents allows you to represent the components of a particulardate, such as hour, minute, day, year, and so on.

NSDateComponents类允许我们表示某一特定日期的一部分,如小时、分钟、日、年等。

 

In addition to these classes, NSTimeZone allows you to represent ageopolitical region’s time zone information.

此外,NSTimeZone类允许我们为表示的地理区域的时区信息。

It eases the task of working across different time zones and performingcalculations that may be affected by daylight savings time transitions.

NSTimeZone类简化了与以下两个因素相关的工作:

多时区(如:跨时区)和夏令时()

 

Creating and Using Date Objects toRepresent Absolute Points in Time

 

创建和使用“日期对象”来表示绝对时间点

Date objects represent dates and times in Cocoa.

在Cocoa 框架中 “Date对象”表示日期和时间。

Date objects allow you to store absolute points in time which aremeaningful across locales, calendars and timezones.

Date对象允许我们存储绝对时间点,这对跨区域、日历、时区相关的工作很有帮助。

 

Relevant Chapters: “Dates”

相关章节:“日期”

 

Working with Calendars and DateComponents

 

Date components allow you to break a date down into the various partsthat comprise it, such as day, month, year, hour, and so on. Calendarsrepresent a particular form of reckoning time, such as the Gregorian calendaror the Chinese calendar.

日期组件(Date components),允许我们将一完整的时期分解为多个部分,如日、月、年、小时等。日历(Calendars)代表一个特殊时间形式,如公历(也称阳历)或中国农历(也称阴历)。

 

Calendar objects allow you to convert between date objects and datecomponent objects, as well as from one calendar to another.

日历对象允许我们进行以下两种转换:

·        日期对象和日期组件对象之间的转换

·        不同的日历(公历或农历)之间的转换

Relevant Chapters:“Calendars, Date Components, and Calendar Units”

Performing Date and TimeCalculations

进行日期和时间相关的计算

Calendars and date components allow you to perform calculations such asthe number of days or hours between two dates or finding the Sunday in thecurrent week.

日历和日期组件可让我们执行诸如天数、小时数的计算,以及,找出本周内的周天。

通过计算(无需我们计算,只需我们调用api即可)得出本周内的周天是几月几号

 

You can also add components to a date or check when a date falls.

 

我们还可以为日期添加组件、以及检查的日期是否正确。

Relevant Chapters: “Calendrical Calculations”

Working with Different Time Zones

Time zone objects allow you to present absolute times as local—that is,wall clock—time.

时区对象允许我们把绝对时间表示为本地时间,就像,墙上的挂钟时间(除非有人喜欢把家里的挂钟故意调整)。

In addition to time offsets, they also keep track of daylight savingtime differences.

除了设置时间偏移之外,他们(时区对象)还处理夏令时相关的事情(如有的地区在夏令时而有的地区不在)。

Proper use of time zone objects can avoid issues such as miscalculationof elapsed time due to daylight saving time transitions or the user moving to adifferent time zone.

正确使用时区对象,可避免类似于下面的问题:

由于夏令时转换导致时间的计算出错问题

由于用户在不同的时区间移动导致时间的计算出错问题

 

Relevant Chapters: “Using Time Zones”

How to Use this Document

If your application keeps track of dates and times, read from “Dates” to“Using Time Zones.”