Movatterモバイル変換


[0]ホーム

URL:


Jump to content
WikibooksThe Free Textbook Project
Search

JavaScript/Dates

From Wikibooks, open books for an open world
<JavaScript
Strings
Arrays


Strings
Arrays


In JavaScript, aDate is an object. Hence it must be explicitly created with thenew operator.

Date contains a value that represents the number of milliseconds that have elapsed since January 1, 1970 UTC. It's worth mentioning what it does not contain: There is no information about a timezone within the object. Nevertheless, you can convert it to an appropriate string of any arbitrary time zone. Other methods can select parts like the month or the day of the week, or you can use the number for any computation, and more.

Constructor

[edit |edit source]

The default constructor creates theDate object as of the current point in timeof your computer.

constcurrentMilliSeconds=newDate();// creates a new Date object as of 'now'alert(currentMilliSeconds);// implicit conversion to stringalert(currentMilliSeconds.toString());// explicit conversion to stringalert(currentMilliSeconds.valueOf());// the real value

You can pass parameters to the constructor to generate a certainDate object.

// 1000 ms = 1 second after the beginning of JavaScript timeconstpointInTime_1=newDate(1000);alert(pointInTime_1);// begin of last minute in last centuryconstpointInTime_2=newDate(1999,11,31,23,59);alert(pointInTime_2);

Methods

[edit |edit source]

Some often usedmethods ofDate are:

Static methods

  • Date.now(): Returns the number of milliseconds since January 1, 1970, 00:00:00UTC calibrated (plus/minus some hours) to the timezone ofyour computer.
  • Date.UTC(<parameters>): Returns the number of milliseconds since January 1, 1970, 00:00:00UTC.
  • Date.parse(text): Parsing of strings withDate.parse() is strongly discouraged due to browser differences and inconsistencies.

Instance methods

  • toISOString(): Returns a string in ISO 8601 format.
  • getFullYear(): Returns the full 4-digit year.
  • getMonth(): Returns the current month. [0 - 11]
  • getDate(): Returns the day of the month. [1 - 31]
  • getDay(): Returns the day of the week. [0 - 6]. Sunday is 0, with the other days of the week taking the next value.
  • getHours(): Returns hours [0 - 23] based on a 24-hour clock.
  • getMinutes(): Returns minutes. [0 - 59]
  • getSeconds(): Returns seconds. [0 - 59]
  • getTime(): Returns the time in milliseconds since January 1, 1970.
  • valueOf(): Returns the time in milliseconds since January 1, 1970. Equivalent to getTime().
  • getTimezoneOffset(): Returns the difference in minutes between UTC and local time.
  • setFullYear(year): Stores the full 4-digit year within the Date object.
  • setMonth(month, day): Sets the month, and optionally the day within the month. '0' is January, ...

'As Integer'

[edit |edit source]

TheDate can be returned as an integer by the methodvalueOf() or by prefixing the constructor with a+ sign, e.g., to use it for "seeding" a PRNG (Pseudo Random Number Generator) or to perform calculations.

constdateAsInteger_1=newDate().valueOf();alert(dateAsInteger_1);constdateAsInteger_2=+newDate();alert(dateAsInteger_2);

Timezones

[edit |edit source]

TheDate object purely contains a whole number (Integer). It does not know anything about timezones. As long as you don't specify timezones in any form, you work in your local timezone.

But sometimes it's necessary to consider timezones.

// Example 1// Create it in UTC: 27th of January, midnightconstdate_1=newDate(Date.UTC(2020,00,27,0,0,0));alert(date_1);// show it in another timezone (Jakarta is +7) ...constjakartaTime=newIntl.DateTimeFormat('en-GB',{timeZone:'Asia/Jakarta',dateStyle:'full',timeStyle:'long'}).format(date_1);alert(jakartaTime);// ... the original value has not changedalert(date_1);
// Example 2// assume we are in New York timezone (-5 against UTC)constdate_2=newDate();console.log(date_2.toString());// show it in another timezone (Los Angeles is -8 against UTC)constlaTime=newIntl.DateTimeFormat('en-GB',{timeZone:'America/Los_Angeles',dateStyle:'full',timeStyle:'long'}).format(date_2);console.log(laTime);// ... the internal value has not changedconsole.log(date_2.toString());

New API: Temporal

[edit |edit source]

The objectDate has some inconsistencies and shortcomings, especially: weak timezone support, no support for non-Gregorian calendars, missing calendar functions, and more. Possibly they will be fixed in a newTemporal API in one of the subsequent JavaScript versions.

Exercises

[edit |edit source]
... are available on another page (click here).
Retrieved from "https://en.wikibooks.org/w/index.php?title=JavaScript/Dates&oldid=4237059"
Category:

[8]ページ先頭

©2009-2025 Movatter.jp