Class
JSDate
public final class JSDate: JSBridgedClass
A wrapper around the JavaScript Date
class that
exposes its properties in a type-safe way. This doesn't 100% match the JS API, for example
getMonth
/setMonth
etc accessor methods are converted to properties, but the rest of it matches
in the naming. Parts of the JavaScript Date
API that are not consistent across browsers and JS
implementations are not exposed in a type-safe manner, you should access the underlying jsObject
property if you need those.
Relationships
Conforms To
JSBridgedClass
Conform to this protocol when your Swift class wraps a JavaScript class.
Comparable
Initializers
init(millisecondsSinceEpoch:)
public init(millisecondsSinceEpoch: Double? = nil)
Creates a new instance of the JavaScript Date
class with a given amount of milliseconds
that passed since midnight 01 January 1970 UTC.
init(year:monthIndex:day:hours:minutes:seconds:milliseconds:)
public init(
year: Int,
monthIndex: Int,
day: Int = 1,
hours: Int = 0,
minutes: Int = 0,
seconds: Int = 0,
milliseconds: Int = 0
)
According to the standard, monthIndex
is zero-indexed, where 11
is December. day
represents a day of the month starting at 1
.
init(unsafelyWrapping:)
public init(unsafelyWrapping jsObject: JSObject)
Properties
constructor
public static let constructor = JSObject.global.Date.function
The constructor function used to create new Date
objects.
minutes
public var minutes: Int
The amount of minutes in this hours from 0..59
range in local time zone.
seconds
public var seconds: Int
The amount of seconds in this minute from 0..59
range in local time zone.
milliseconds
public var milliseconds: Int
The amount of milliseconds in this second 0..999
range in local time zone.
utcHours
public var utcHours: Int
The amount of hours in this day from 0..23
range in the UTC time zone.
utcMinutes
public var utcMinutes: Int
The amount of minutes in this hours from 0..59
range in the UTC time zone.
utcSeconds
public var utcSeconds: Int
The amount of seconds in this minute from 0..59
range in the UTC time zone.
utcMilliseconds
public var utcMilliseconds: Int
The amount of milliseconds in this second 0..999
range in the UTC time zone.
timezoneOffset
public var timezoneOffset: Int
Offset in minutes between the local time zone and UTC.
Methods
toISOString()
public func toISOString() -> String
Returns a string conforming to ISO 8601 that contains date and time, e.g.
"2020-09-15T08:56:54.811Z"
.
toLocaleDateString()
public func toLocaleDateString() -> String
Returns a string with date parts in a format defined by user's locale, e.g. "9/15/2020"
.
toLocaleTimeString()
public func toLocaleTimeString() -> String
Returns a string with time parts in a format defined by user's locale, e.g. "10:04:14"
.
toUTCString()
public func toUTCString() -> String
now()
public static func now() -> Double
Number of milliseconds since midnight 01 January 1970 UTC to the present moment ignoring leap seconds.
valueOf()
public func valueOf() -> Double
Number of milliseconds since midnight 01 January 1970 UTC to the given date ignoring leap seconds.