20200619--iOS之Date结构体(时间类)

本文详细介绍了iOS中Date结构体的使用,包括创建日期、获取时间界限、日期比较、时间间隔计算等。Date表示一个特定的时间点,不依赖于任何日历或时区,其默认是零时区的时间戳。通过格式化可以转换为不同时区的字符串,但Date本身不支持直接修改时区。文章还涵盖了Date的构造器、实例方法和运算符等功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Structure

Date

A specific point in time, independent of any calendar or time zone.

        --一个特定的时间点,独立于任何日历或时区。

默认是零时区的时间戳,不能修改时区,你只能根据时间戳转换成其他时区的时间的字符串显示。

而formater就是默认是当地的时区,回根据时间戳转换成当地的时间表示,但是转换回Date类型时,Date还是零时区的表示方式,即Date的默认表示方式是不能修改的,只能通过format转换为当地的时间表示,这两者唯一不变的就是时间戳而已。

即是时间戳的不同表示形式罢了,Date不能修改自身默认的描述罢了(默认通过零时区来描述时间戳)。

Declaration            --声明

struct Date

Overview          --概览

A Date value encapsulate a single point in time, independent of any particular calendrical system or time zone. Date values represent a time interval relative to an absolute reference date.

           --日期值封装一个时间点,独立于任何特定的日历系统或时区。日期值表示的距离绝对的日期参考值 的时间间隔

The Date structure provides methods for comparing dates, calculating the time interval between two dates, and creating a new date from a time interval relative to another date. Use date values in conjunction with DateFormatter instances to create localized representations of dates and times and with Calendar instances to perform calendar arithmetic.

        --日期结构提供了比较日期、计算两个日期之间的时间间隔以及从相对于另一个日期的时间间隔创建新日期的方法。将日期值DateFormatter实例结合使用可创建日期和时间的本地化表示形式,并与日历实例结合使用可执行日历计算。

Date bridges to the NSDate class. You can use these interchangeably in code that interacts with Objective-C APIs.

        --Date桥接到NSDate类。你可以在与Objective-C api交互的代码中交替使用这些。

Topics       --专题

Creating a Date       --创建日期

init()

Creates a date value initialized to the current date and time.

       --创建初始化为当前日期和时间的日期值。

 

init(timeIntervalSinceNow: TimeInterval)

Creates a date value initialized relative to the current date and time by a given number of seconds.

         --通过给定的秒数,创建一个相对于当前日期的日期值。

 

init(timeInterval: TimeInterval, since: Date)

Creates a date value initialized relative to another given date by a given number of seconds.

        --创建一个相对于另一个给定日期的日期值,时间间隔为参数中的秒数。

 

init(timeIntervalSinceReferenceDate: TimeInterval)

Creates a date value initialized relative to 00:00:00 UTC on 1 January 2001 by a given number of seconds.

             --创建相对于2001年1月1日00:00:00 UTC初始化的给定秒数的日期值。

 

init(timeIntervalSince1970: TimeInterval)

Creates a date value initialized relative to 00:00:00 UTC on 1 January 1970 by a given number of seconds.

           --创建相对于1970年1月1日00:00:00 UTC初始化的给定秒数的日期值。

 

Getting Temporal Boundaries         --获得时间界限

static let distantFuture: Date

A date value representing a date in the distant future.

       --表示将来某个日期的日期值。类似于bool值的使用,供定时器参考

static let distantPast: Date

A date value representing a date in the distant past.

     --表示很久以前某个日期的日期值。

 

Comparing Dates       --日期之间的比较

static func < (Date, Date) -> Bool

Returns a Boolean that is true if the left hand date is earlier in time than the right hand date.

       --如果左侧日期的时间早于右侧日期,则返回为true的布尔值。

static func == (Date, Date) -> Bool

Returns a Boolean that is true if the two date values represent the same point in time.

      --如果两个日期值表示同一时间点,则返回true的布尔值。

func compare(Date) -> ComparisonResult

Compares two date values.

         --比较两个日期值。

 

Getting Time Intervals       --获取时间戳

func timeIntervalSince(Date) -> TimeInterval

Returns the interval between the receiver and another given date.

       --返回接收者与另一个给定日期之间的时间间隔。

 

var timeIntervalSinceNow: TimeInterval

The time interval between the date value and the current date and time.

        --日期值与当前日期和时间之间的时间间隔。

 

var timeIntervalSinceReferenceDate: TimeInterval

The interval between the date value and 00:00:00 UTC on 1 January 2001.

       --日期值与2001年1月1日00:00:00 UTC之间的间隔。

 

var timeIntervalSince1970: TimeInterval

The interval between the date value and 00:00:00 UTC on 1 January 1970.

        --日期值与1970年1月1日00:00:00 UTC之间的间隔。

static var timeIntervalSinceReferenceDate: TimeInterval

The interval between 00:00:00 UTC on 1 January 2001 and the current date and time.

      --2001年1月1日00:00:00 UTC与当前日期和时间之间的间隔。

static let timeIntervalBetween1970AndReferenceDate: TimeInterval

The number of seconds from 1 January 1970 to the reference date, 1 January 2001.

     --1970年1月1日至参考日期2001年1月1日的秒数。

 

Adding or Subtracting a Time Interval     --添加或减去一个时间间隔

static func != (Date, Date) -> Bool

Returns a Boolean value indicating whether two values are not equal.

static func + (Date, TimeInterval) -> Date

Returns a date with a specified amount of time added to it.

static func += (inout Date, TimeInterval)

Adds a time interval to a date.

static func - (Date, TimeInterval) -> Date

Returns a date with a specified amount of time subtracted from it.

static func -= (inout Date, TimeInterval)

Subtracts a time interval from a date.

static func < (Date, Date) -> Bool

Returns a Boolean that is true if the left hand date is earlier in time than the right hand date.

static func <= (Date, Date) -> Bool

Returns a Boolean value indicating whether the value of the first argument is less than or equal to that of the second argument.

static func == (Date, Date) -> Bool

Returns a Boolean that is true if the two date values represent the same point in time.

static func >= (Date, Date) -> Bool

Returns a Boolean value indicating whether the value of the first argument is greater than or equal to that of the second argument.

 

Describing Dates           --描述日期

var description: String

A textual description of the date value.

func description(with: Locale?) -> String

Returns a string representation of the date using the given locale.

var debugDescription: String

A textual description of the date suitable for debugging.

var customMirror: Mirror

A mirror that reflects the date.

var hashValue: Int

The computed hash value of the date.

var customPlaygroundQuickLook: PlaygroundQuickLook

A custom playground Quick Look for the date.

Deprecated

 

Using Reference Types         --使用引用类型

class NSDate

A representation of a specific point in time that bridges to Date; use NSDate when you need reference semantics or other Foundation-specific behavior.

typealias Date.ReferenceType

An alias for this value type's equivalent reference type.

 

Type Aliases        --类型别名

typealias Date.Stride

 

Initializers            --构造器

init(from: Decoder)

 

Instance Methods        --实例方法

func addTimeInterval(TimeInterval)

Adds a time interval to this date.

func addingTimeInterval(TimeInterval) -> Date

Creates a new date value by adding a time interval to this date.

func advanced(by: TimeInterval) -> Date

func distance(to: Date) -> TimeInterval

func encode(to: Encoder)

func hash(into: inout Hasher)

 

Operator Functions          --运算符

static func ... (Date) -> PartialRangeFrom<Date>

Returns a partial range extending upward from a lower bound.

static func ... (Date) -> PartialRangeThrough<Date>

Returns a partial range up to, and including, its upper bound.

static func ... (Date, Date) -> ClosedRange<Date>

Returns a closed range that contains both of its bounds.

static func ..< (Date) -> PartialRangeUpTo<Date>

Returns a partial range up to, but not including, its upper bound.

static func ..< (Date, Date) -> Range<Date>

Returns a half-open range that contains its lower bound but not its upper bound.

static func > (Date, Date) -> Bool

static func > (Date, Date) -> Bool

Returns a Boolean value indicating whether the value of the first argument is greater than that of the second argument.

 

Relationships         --继承关系

Conforms To

See Also

Date Representations

struct DateInterval

The span of time between a specific start date and end date.

typealias TimeInterval

A number of seconds.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值