Date

  • Enlace amigable (slug) de la revisión: Referencia_de_JavaScript_1.5/Objetos_globales/Date
  • Título de la revisión: Date
  • Id de la revisión: 181308
  • Creada:
  • Creador: Talisker
  • ¿Es la revisión actual? No
  • Comentario /* Static methods */

Contenido de la revisión

Summary

Core Object

Lets you work with dates and times.

Created by

The Date constructor:

new Date()
new Date(milliseconds)
new Date(dateString)
new Date(yr_num, mo_num, day_num
        [, hr_num, min_num, sec_num, ms_num])

Parameters

milliseconds 
Integer value representing the number of milliseconds since 1 January 1970 00:00:00 UTC.
dateString 
String value representing a date. The string should be in a format recognized by the parse method.
yr_num, mo_num, day_num 
Integer values representing part of a date. As an integer value, the month is represented by 0 to 11 with 0=January and 11=December.
hr_num, min_num, sec_num, ms_num 
Integer values representing part of a date.

Description

If you supply no arguments, the constructor creates a Date object for today's date and time according to local time. If you supply some arguments but not others, the missing arguments are set to 0. If you supply any arguments, you must supply at least the year, month, and day. You can omit the hours, minutes, seconds, and milliseconds.

The date is measured in milliseconds since midnight 01 January, 1970 UTC. A day holds 86,400,000 milliseconds. The Date object range is -100,000,000 days to 100,000,000 days relative to 01 January, 1970 UTC.

The Date object provides uniform behavior across platforms.

The Date object supports a number of UTC (universal) methods, as well as local time methods. UTC, also known as Greenwich Mean Time (GMT), refers to the time as set by the World Time Standard. The local time is the time known to the computer where JavaScript is executed.

For compatibility with millennium calculations (in other words, to take into account the year 2000), you should always specify the year in full; for example, use 1998, not 98. To assist you in specifying the complete year, JavaScript includes the methods getFullYear, setFullYear, getUTCFullYear, and setUTCFullYear.

The following example returns the time elapsed between timeA and timeB in milliseconds.

timeA = new Date();
// Statements here to take some action.
timeB = new Date();
timeDifference = timeB - timeA;

Propiedades

  • constructor: Especifica la función que crea un prototipo del objeto.
  • prototype: Permite añadir propiedades a un objeto Date.

Métodos Estáticos

  • now: Devuelve el valor numérico correspondiente a la hora actual.
  • parse: Transforma una cadena que representa una fecha, y devuelve el número de milisegundos desde el 1 de enero de 1970, hora local 00:00:00.
  • UTC: Acepta los mismos parámetros que la forma más larga del constructor, y devuelve el número de milisegundos en un objeto Date desde el 1 de enero de 1970, hora universal 00:00:00.

Methods

  • getDate: Returns the day of the month for the specified date according to local time.
  • getDay: Returns the day of the week for the specified date according to local time.
  • getFullYear: Returns the year of the specified date according to local time.
  • getHours: Returns the hour in the specified date according to local time.
  • getMilliseconds: Returns the milliseconds in the specified date according to local time.
  • getMinutes: Returns the minutes in the specified date according to local time.
  • getMonth: Returns the month in the specified date according to local time.
  • getSeconds: Returns the seconds in the specified date according to local time.
  • getTime: Returns the numeric value corresponding to the time for the specified date according to universal time.
  • getTimezoneOffset: Returns the time-zone offset in minutes for the current locale.
  • getUTCDate: Returns the day (date) of the month in the specified date according to universal time.
  • getUTCDay: Returns the day of the week in the specified date according to universal time.
  • getUTCFullYear: Returns the year in the specified date according to universal time.
  • getUTCHours: Returns the hours in the specified date according to universal time.
  • getUTCMilliseconds: Returns the milliseconds in the specified date according to universal time.
  • getUTCMinutes: Returns the minutes in the specified date according to universal time.
  • getUTCMonth: Returns the month in the specified date according to universal time.
  • getUTCSeconds: Returns the seconds in the specified date according to universal time.
  • getYear {{template.Deprecated_inline()}} : Returns the year in the specified date according to local time. Use getFullYear instead.
  • setDate: Sets the day of the month for a specified date according to local time.
  • setFullYear: Sets the full year for a specified date according to local time.
  • setHours: Sets the hours for a specified date according to local time.
  • setMilliseconds: Sets the milliseconds for a specified date according to local time.
  • setMinutes: Sets the minutes for a specified date according to local time.
  • setMonth: Sets the month for a specified date according to local time.
  • setSeconds: Sets the seconds for a specified date according to local time.
  • setTime: Sets the value of the Date object according to local time.
  • setUTCDate: Sets the day of the month for a specified date according to universal time.
  • setUTCFullYear: Sets the full year for a specified date according to universal time.
  • setUTCHours: Sets the hour for a specified date according to universal time.
  • setUTCMilliseconds: Sets the milliseconds for a specified date according to universal time.
  • setUTCMinutes: Sets the minutes for a specified date according to universal time.
  • setUTCMonth: Sets the month for a specified date according to universal time.
  • setUTCSeconds: Sets the seconds for a specified date according to universal time.
  • setYear {{template.Deprecated_inline()}} : Sets the year for a specified date according to local time. Use setFullYear instead.
  • toGMTString {{template.Deprecated_inline()}}: Converts a date to a string, using the Internet GMT conventions. Use toUTCString instead.
  • toLocaleString: Converts a date to a string, using the current locale's conventions. Overrides the Object.toLocaleString method.
  • toLocaleDateString: Returns the "date" portion of the Date as a string, using the current locale's conventions.
  • toLocaleTimeString: Returns the "time" portion of the Date as a string, using the current locale's conventions.
  • toSource: Returns an object literal representing the specified Date object; you can use this value to create a new object. Overrides the Object.toSource method.
  • toString: Returns a string representing the specified Date object. Overrides the Object.toString method.
  • toUTCString: Converts a date to a string, using the universal time convention.
  • valueOf: Returns the primitive value of a Date object. Overrides the Object.valueOf method.

In addition, this object inherits the watch and unwatch methods from Object.

Examples

Example: Several ways to assign dates

The following examples show several ways to assign dates:

today = new Date();
birthday = new Date("December 17, 1995 03:24:00");
birthday = new Date(1995,11,17);
birthday = new Date(1995,11,17,3,24,0);

Example: Calculating elapsed time

The following examples show how to determine the elapsed time between two dates:

// using static methods
var start = Date.now();
// the event you'd like to time goes here:
doSomethingForALongTime();
var end = Date.now();
var elapsed = end - start; // time in milliseconds
// if you have Date objects
var start = new Date();
// the event you'd like to time goes here:
doSomethingForALongTime();
var end = new Date();
var elapsed = end.getTime() - start.getTime(); // time in milliseconds
{{ wiki.languages( { "en": "en/Core_JavaScript_1.5_Reference/Global_Objects/Date", "fr": "fr/R\u00e9f\u00e9rence_de_JavaScript_1.5_Core/Objets_globaux/Date", "ja": "ja/Core_JavaScript_1.5_Reference/Global_Objects/Date", "pl": "pl/Dokumentacja_j\u0119zyka_JavaScript_1.5/Obiekty/Date", "zh-cn": "cn/Core_JavaScript_1.5_Reference/Global_Objects/Date" } ) }}

Fuente de la revisión

<p>
</p>
<h3 name="Summary"> Summary </h3>
<p><b>Core Object</b>
</p><p>Lets you work with dates and times.
</p>
<h3 name="Created_by"> Created by </h3>
<p>The <code>Date</code> constructor:
</p>
<pre class="eval">new Date()
new Date(<i>milliseconds</i>)
new Date(<i>dateString</i>)
new Date(<i>yr_num</i>, <i>mo_num</i>, <i>day_num</i>
        [, <i>hr_num</i>, <i>min_num</i>, <i>sec_num</i>, <i>ms_num</i>])
</pre>
<h3 name="Parameters"> Parameters </h3>
<dl><dt> <code>milliseconds</code> </dt><dd> Integer value representing the number of milliseconds since 1 January 1970 00:00:00 UTC.
</dd></dl>
<dl><dt> <code>dateString</code> </dt><dd> String value representing a date. The string should be in a format recognized by the <a href="es/Referencia_de_JavaScript_1.5/Objetos_globales/Date/parse">parse</a> method.
</dd></dl>
<dl><dt> <code>yr_num, mo_num, day_num</code> </dt><dd> Integer values representing part of a date. As an integer value, the month is represented by 0 to 11 with 0=January and 11=December.
</dd></dl>
<dl><dt> <code>hr_num, min_num, sec_num, ms_num</code> </dt><dd> Integer values representing part of a date.
</dd></dl>
<h3 name="Description"> Description </h3>
<p>If you supply no arguments, the constructor creates a <code>Date</code> object for today's date and time according to local time. If you supply some arguments but not others, the missing arguments are set to 0. If you supply any arguments, you must supply at least the year, month, and day. You can omit the hours, minutes, seconds, and milliseconds.
</p><p>The date is measured in milliseconds since midnight 01 January, 1970 UTC. A day holds 86,400,000 milliseconds. The Date object range is -100,000,000 days to 100,000,000 days relative to 01 January, 1970 UTC.
</p><p>The <code>Date</code> object provides uniform behavior across platforms.
</p><p>The <code>Date</code> object supports a number of UTC (universal) methods, as well as local time methods. UTC, also known as Greenwich Mean Time (GMT), refers to the time as set by the World Time Standard. The local time is the time known to the computer where JavaScript is executed.
</p><p>For compatibility with millennium calculations (in other words, to take into account the year 2000), you should always specify the year in full; for example, use 1998, not 98. To assist you in specifying the complete year, JavaScript includes the methods <code><a href="es/Core_JavaScript_1.5_Reference/Global_Objects/Date/getFullYear">getFullYear</a></code>, <code><a href="es/Core_JavaScript_1.5_Reference/Global_Objects/Date/setFullYear">setFullYear</a></code>, <code><a href="es/Core_JavaScript_1.5_Reference/Global_Objects/Date/getUTCFullYear">getUTCFullYear</a></code>, and <code><a href="es/Core_JavaScript_1.5_Reference/Global_Objects/Date/setUTCFullYear">setUTCFullYear</a></code>.
</p><p>The following example returns the time elapsed between <code>timeA</code> and <code>timeB</code> in milliseconds.
</p>
<pre class="eval">timeA = new Date();
// Statements here to take some action.
timeB = new Date();
timeDifference = timeB - timeA;
</pre>
<h3 name="Propiedades"> Propiedades </h3>
<ul><li> <a href="es/Referencia_de_JavaScript_1.5/Objetos_globales/Date/constructor">constructor</a>: Especifica la función que crea un prototipo del objeto.
</li><li> <a href="es/Referencia_de_JavaScript_1.5/Objetos_globales/Date/prototype">prototype</a>: Permite añadir propiedades a un objeto <code>Date</code>.
</li></ul>
<h3 name="M.C3.A9todos_Est.C3.A1ticos"> Métodos Estáticos </h3>
<ul><li> <a href="es/Referencia_de_JavaScript_1.5/Objetos_globales/Date/now">now</a>: Devuelve el valor numérico correspondiente a la hora actual.
</li><li> <a href="es/Referencia_de_JavaScript_1.5/Objetos_globales/Date/parse">parse</a>: Transforma una cadena que representa una fecha, y devuelve el número de milisegundos desde el 1 de enero de 1970, hora local 00:00:00.
</li><li> <a href="es/Referencia_de_JavaScript_1.5/Objetos_globales/Date/UTC">UTC</a>: Acepta los mismos parámetros que la forma más larga del constructor, y devuelve el número de milisegundos en un objeto <code>Date</code> desde el 1 de enero de 1970, hora universal 00:00:00.
</li></ul>
<h3 name="Methods"> Methods </h3>
<ul><li> <a href="es/Referencia_de_JavaScript_1.5/Objetos_globales/Date/getDate">getDate</a>: Returns the day of the month for the specified date according to local time.
</li><li> <a href="es/Referencia_de_JavaScript_1.5/Objetos_globales/Date/getDay">getDay</a>: Returns the day of the week for the specified date according to local time.
</li><li> <a href="es/Referencia_de_JavaScript_1.5/Objetos_globales/Date/getFullYear">getFullYear</a>: Returns the year of the specified date according to local time.
</li><li> <a href="es/Referencia_de_JavaScript_1.5/Objetos_globales/Date/getHours">getHours</a>: Returns the hour in the specified date according to local time.
</li><li> <a href="es/Referencia_de_JavaScript_1.5/Objetos_globales/Date/getMilliseconds">getMilliseconds</a>: Returns the milliseconds in the specified date according to local time.
</li><li> <a href="es/Referencia_de_JavaScript_1.5/Objetos_globales/Date/getMinutes">getMinutes</a>: Returns the minutes in the specified date according to local time.
</li><li> <a href="es/Referencia_de_JavaScript_1.5/Objetos_globales/Date/getMonth">getMonth</a>: Returns the month in the specified date according to local time.
</li><li> <a href="es/Referencia_de_JavaScript_1.5/Objetos_globales/Date/getSeconds">getSeconds</a>: Returns the seconds in the specified date according to local time.
</li><li> <a href="es/Referencia_de_JavaScript_1.5/Objetos_globales/Date/getTime">getTime</a>: Returns the numeric value corresponding to the time for the specified date according to universal time.
</li><li> <a href="es/Referencia_de_JavaScript_1.5/Objetos_globales/Date/getTimezoneOffset">getTimezoneOffset</a>: Returns the time-zone offset in minutes for the current locale.
</li><li> <a href="es/Referencia_de_JavaScript_1.5/Objetos_globales/Date/getUTCDate">getUTCDate</a>: Returns the day (date) of the month in the specified date according to universal time.
</li><li> <a href="es/Referencia_de_JavaScript_1.5/Objetos_globales/Date/getUTCDay">getUTCDay</a>: Returns the day of the week in the specified date according to universal time.
</li><li> <a href="es/Referencia_de_JavaScript_1.5/Objetos_globales/Date/getUTCFullYear">getUTCFullYear</a>: Returns the year in the specified date according to universal time.
</li><li> <a href="es/Referencia_de_JavaScript_1.5/Objetos_globales/Date/getUTCHours">getUTCHours</a>: Returns the hours in the specified date according to universal time.
</li><li> <a href="es/Referencia_de_JavaScript_1.5/Objetos_globales/Date/getUTCMilliseconds">getUTCMilliseconds</a>: Returns the milliseconds in the specified date according to universal time.
</li><li> <a href="es/Referencia_de_JavaScript_1.5/Objetos_globales/Date/getUTCMinutes">getUTCMinutes</a>: Returns the minutes in the specified date according to universal time.
</li><li> <a href="es/Referencia_de_JavaScript_1.5/Objetos_globales/Date/getUTCMonth">getUTCMonth</a>: Returns the month in the specified date according to universal time.
</li><li> <a href="es/Referencia_de_JavaScript_1.5/Objetos_globales/Date/getUTCSeconds">getUTCSeconds</a>: Returns the seconds in the specified date according to universal time.
</li><li> <a href="es/Referencia_de_JavaScript_1.5/Objetos_globales/Date/getYear">getYear</a> {{template.Deprecated_inline()}} : Returns the year in the specified date according to local time. Use <a href="es/Core_JavaScript_1.5_Reference/Global_Objects/Date/getFullYear">getFullYear</a> instead.
</li><li> <a href="es/Referencia_de_JavaScript_1.5/Objetos_globales/Date/setDate">setDate</a>: Sets the day of the month for a specified date according to local time.
</li><li> <a href="es/Referencia_de_JavaScript_1.5/Objetos_globales/Date/setFullYear">setFullYear</a>: Sets the full year for a specified date according to local time.
</li><li> <a href="es/Referencia_de_JavaScript_1.5/Objetos_globales/Date/setHours">setHours</a>: Sets the hours for a specified date according to local time.
</li><li> <a href="es/Referencia_de_JavaScript_1.5/Objetos_globales/Date/setMilliseconds">setMilliseconds</a>: Sets the milliseconds for a specified date according to local time.
</li><li> <a href="es/Referencia_de_JavaScript_1.5/Objetos_globales/Date/setMinutes">setMinutes</a>: Sets the minutes for a specified date according to local time.
</li><li> <a href="es/Referencia_de_JavaScript_1.5/Objetos_globales/Date/setMonth">setMonth</a>: Sets the month for a specified date according to local time.
</li><li> <a href="es/Referencia_de_JavaScript_1.5/Objetos_globales/Date/setSeconds">setSeconds</a>: Sets the seconds for a specified date according to local time.
</li><li> <a href="es/Referencia_de_JavaScript_1.5/Objetos_globales/Date/setTime">setTime</a>: Sets the value of the <code>Date</code> object according to local time.
</li><li> <a href="es/Referencia_de_JavaScript_1.5/Objetos_globales/Date/setUTCDate">setUTCDate</a>: Sets the day of the month for a specified date according to universal time.
</li><li> <a href="es/Referencia_de_JavaScript_1.5/Objetos_globales/Date/setUTCFullYear">setUTCFullYear</a>: Sets the full year for a specified date according to universal time.
</li><li> <a href="es/Referencia_de_JavaScript_1.5/Objetos_globales/Date/setUTCHours">setUTCHours</a>: Sets the hour for a specified date according to universal time.
</li><li> <a href="es/Referencia_de_JavaScript_1.5/Objetos_globales/Date/setUTCMilliseconds">setUTCMilliseconds</a>: Sets the milliseconds for a specified date according to universal time.
</li><li> <a href="es/Referencia_de_JavaScript_1.5/Objetos_globales/Date/setUTCMinutes">setUTCMinutes</a>: Sets the minutes for a specified date according to universal time.
</li><li> <a href="es/Referencia_de_JavaScript_1.5/Objetos_globales/Date/setUTCMonth">setUTCMonth</a>: Sets the month for a specified date according to universal time.
</li><li> <a href="es/Referencia_de_JavaScript_1.5/Objetos_globales/Date/setUTCSeconds">setUTCSeconds</a>: Sets the seconds for a specified date according to universal time.
</li><li> <a href="es/Referencia_de_JavaScript_1.5/Objetos_globales/Date/setYear">setYear</a> {{template.Deprecated_inline()}} : Sets the year for a specified date according to local time. Use <a href="es/Core_JavaScript_1.5_Reference/Global_Objects/Date/setFullYear">setFullYear</a> instead.
</li><li> <a href="es/Referencia_de_JavaScript_1.5/Objetos_globales/Date/toGMTString">toGMTString</a> {{template.Deprecated_inline()}}: Converts a date to a string, using the Internet GMT conventions. Use <a href="es/Core_JavaScript_1.5_Reference/Global_Objects/Date/toUTCString">toUTCString</a> instead.
</li><li> <a href="es/Referencia_de_JavaScript_1.5/Objetos_globales/Date/toLocaleString">toLocaleString</a>: Converts a date to a string, using the current locale's conventions. Overrides the <a href="es/Core_JavaScript_1.5_Reference/Global_Objects/Object/toLocaleString">Object.toLocaleString</a> method.
</li><li> <a href="es/Referencia_de_JavaScript_1.5/Objetos_globales/Date/toLocaleDateString">toLocaleDateString</a>: Returns the "date" portion of the Date as a string, using the current locale's conventions.
</li><li> <a href="es/Referencia_de_JavaScript_1.5/Objetos_globales/Date/toLocaleTimeString">toLocaleTimeString</a>: Returns the "time" portion of the Date as a string, using the current locale's conventions.
</li><li> <a href="es/Referencia_de_JavaScript_1.5/Objetos_globales/Date/toSource">toSource</a>: Returns an object literal representing the specified <code>Date</code> object; you can use this value to create a new object.  Overrides the <a href="es/Core_JavaScript_1.5_Reference/Global_Objects/Object/toSource">Object.toSource</a> method.
</li><li> <a href="es/Referencia_de_JavaScript_1.5/Objetos_globales/Date/toString">toString</a>: Returns a string representing the specified <code>Date</code> object.  Overrides the <a href="es/Core_JavaScript_1.5_Reference/Global_Objects/Object/toString">Object.toString</a> method.
</li><li> <a href="es/Referencia_de_JavaScript_1.5/Objetos_globales/Date/toUTCString">toUTCString</a>: Converts a date to a string, using the universal time convention.
</li><li> <a href="es/Referencia_de_JavaScript_1.5/Objetos_globales/Date/valueOf">valueOf</a>: Returns the primitive value of a <code>Date</code> object.  Overrides the <a href="es/Referencia_de_JavaScript_1.5/Objetos_globales/Object/valueOf">Object.valueOf</a> method.
</li></ul>
<p>In addition, this object inherits the <a href="es/Referencia_de_JavaScript_1.5/Objetos_globales/Object/watch">watch</a> and <a href="es/Referencia_de_JavaScript_1.5/Objetos_globales/Object/unwatch">unwatch</a> methods from <a href="es/Referencia_de_JavaScript_1.5/Objetos_globales/Object">Object</a>.
</p>
<h3 name="Examples"> Examples </h3>
<h4 name="Example:_Several_ways_to_assign_dates"> Example: Several ways to assign dates </h4>
<p>The following examples show several ways to assign dates:
</p>
<pre class="eval">today = new Date();
birthday = new Date("December 17, 1995 03:24:00");
birthday = new Date(1995,11,17);
birthday = new Date(1995,11,17,3,24,0);
</pre>
<h4 name="Example:_Calculating_elapsed_time"> Example: Calculating elapsed time </h4>
<p>The following examples show how to determine the elapsed time between two dates:
</p>
<pre class="eval">// using static methods
var start = Date.now();
// the event you'd like to time goes here:
doSomethingForALongTime();
var end = Date.now();
var elapsed = end - start; // time in milliseconds
</pre>
<pre class="eval">// if you have Date objects
var start = new Date();
// the event you'd like to time goes here:
doSomethingForALongTime();
var end = new Date();
var elapsed = end.getTime() - start.getTime(); // time in milliseconds
</pre>
<div class="noinclude">
</div>
{{ wiki.languages( { "en": "en/Core_JavaScript_1.5_Reference/Global_Objects/Date", "fr": "fr/R\u00e9f\u00e9rence_de_JavaScript_1.5_Core/Objets_globaux/Date", "ja": "ja/Core_JavaScript_1.5_Reference/Global_Objects/Date", "pl": "pl/Dokumentacja_j\u0119zyka_JavaScript_1.5/Obiekty/Date", "zh-cn": "cn/Core_JavaScript_1.5_Reference/Global_Objects/Date" } ) }}
Revertir a esta revisión