Métodos de fecha de JavaScript


Tabla de contenido

    Mostrar tabla de contenidos


El constructor new Date()

En JavaScript, los objetos de fecha se crean con new Date().

new Date() devuelve un objeto de fecha con la fecha y hora actuales.

Obtener la hora actual

const date = new Date();

Pruébelo usted mismo →

<!DOCTYPE html>
<html>
<body>

<h1>JavaScript Dates</h1>
<h2>Using new Date()</h2>
<p>Create a new date object with the current date and time:</p>

<p id="demo"></p>

<script>
const d = new Date();
document.getElementById("demo").innerHTML = d;
</script>

</body>
</html>

Métodos de obtención de fecha

getFullYear()

Obtener año como un número de cuatro dígitos (aaaa)

getMonth()

Obtener mes como un número (0-11)

getDate()

Obtener día como un número (1-31)

getDay()

Obtener día de la semana como un número (0-6)

getHours()

Obtener hora (0-23)

getMinutes()

Obtener minuto (0-59)

getSeconds()

Consigue el segundo (0-59)

getMilliseconds()

Obtener milisegundo (0-999)

getTime()

Obtener tiempo (milisegundos desde el 1 de enero de 1970)

Nota 1

Los métodos de obtención anteriores devuelven hora local.

La hora universal (UTC) está documentada en la parte inferior de esta página.

Nota 2

Los métodos get devuelven información de objetos de fecha existentes.

En un objeto de fecha, la hora es estática. El "reloj" no está "corriendo".

La hora en un objeto de fecha NO es la misma que la hora actual.


El método getFullYear()

El método getFullYear() devuelve el año de una fecha como un número de cuatro dígitos:

Ejemplos

const d = new Date("2021-03-25");
d.getFullYear();

Pruébelo usted mismo →

<!DOCTYPE html>
<html>
<body>
<h1>JavaScript Dates</h1>
<h2>The getFullYear() Method</h2>
<p>Return the full year of a date object:</p>

<p id="demo"></p>

<script>
const d = new Date("2021-03-25")
document.getElementById("demo").innerHTML = d.getFullYear();
</script>

</body>
</html>
const d = new Date();
d.getFullYear();

Pruébelo usted mismo →

<!DOCTYPE html>
<html>
<body>

<h1>JavaScript Dates</h1>
<h2>The getFullYear() Method</h2>
<p>Return the full year of a date object:</p>

<p id="demo"></p>

<script>
const d = new Date();
document.getElementById("demo").innerHTML = d.getFullYear();
</script>

</body>
</html>

Advertencia !

El código JavaScript antiguo podría utilizar el método no estándar getYear().

Se supone que getYear() devuelve un año de 2 dígitos.

getYear() está en desuso. ¡No lo uses!


El método getMonth()

El método getMonth() devuelve el mes de una fecha como un número (0-11).

Nota

En JavaScript, enero es el mes número 0, febrero es el número 1,...

Finalmente, diciembre es el mes número 11.

Ejemplos

const d = new Date("2021-03-25");
d.getMonth();

Pruébelo usted mismo →

<!DOCTYPE html>
<html>
<body>
<h1>JavaScript Dates</h1>
<h2>The getMonth() Method</h2>
<p>Return the month of a date as a number from 0 to 11.</p>
<p>To get the correct month number, you must add 1:</p>

<p id="demo"></p>

<script>
const d = new Date("2021-03-25");
document.getElementById("demo").innerHTML = d.getMonth() + 1;
</script>

</body>
</html>
const d = new Date();
d.getMonth();

Pruébelo usted mismo →

<!DOCTYPE html>
<html>
<body>
<h1>JavaScript Dates</h1>
<h2>The getMonth() Method</h2>
<p>Return the month of a date as a number from 0 to 11.</p>
<p>To get the correct month number, you must add 1:</p>

<p id="demo"></p>

<script>
const d = new Date();
document.getElementById("demo").innerHTML = d.getMonth() + 1;
</script>

</body>
</html>

Nota

Puede utilizar una serie de nombres para devolver el mes como nombre:

Ejemplos

const months = ["January", "February", "March", "April", "May",
"June", "July", "August", "September", "October",
"November", "December"];

const d = new Date("2021-03-25");
let month = months[d.getMonth()];

Pruébelo usted mismo →

<!DOCTYPE html>
<html>
<body>
<h1>JavaScript Dates</h1>
<h2>JavaScript getMonth()</h2>
<p>Return the month as a number.</p>
<p>You can use an array of names to return the month as a name:</p>

<p id="demo"></p>

<script>
const months = ["January","February","March","April","May","June","July","August","September","October","November","December"];

const d = new Date("2021-03-25");
let month = months[d.getMonth()];
document.getElementById("demo").innerHTML = month;
</script>

</body>
</html>
const months = ["January", "February", "March", "April", "May",
"June", "July", "August", "September", "October",
"November", "December"];

const d = new Date();
let month = months[d.getMonth()];

Pruébelo usted mismo →

<!DOCTYPE html>
<html>
<body>
<h1>JavaScript Dates</h1>
<h2>JavaScript getMonth()</h2>
<p>Return the month as a number.</p>
<p>You can use an array of names to return the month as a name:</p>

<p id="demo"></p>

<script>
const months = ["January","February","March","April","May","June","July","August","September","October","November","December"];

const d = new Date();
let month = months[d.getMonth()];
document.getElementById("demo").innerHTML = month;
</script>

</body>
</html>

El método getDate()

El método getDate() devuelve el día de una fecha como un número (1-31):

Ejemplos

const d = new Date("2021-03-25");
d.getDate();

Pruébelo usted mismo →

<!DOCTYPE html>
<html>
<body>
<h1>JavaScript Dates</h1>
<h2>The getDate() Method</h2>
<p>Return the day of a date as a number (1-31):</p>

<p id="demo"></p>

<script>
const d = new Date("2021-03-25");
document.getElementById("demo").innerHTML = d.getDate();
</script>

</body>
</html>
const d = new Date();
d.getDate();

Pruébelo usted mismo →

<!DOCTYPE html>
<html>
<body>
<h1>JavaScript Dates</h1>
<h2>The getDate() Method</h2>
<p>Return the day of a date as a number (1-31):</p>

<p id="demo"></p>

<script>
const d = new Date();
document.getElementById("demo").innerHTML = d.getDate();
</script>

</body>
</html>


El método getHours()

El método getHours() devuelve las horas de una fecha como un número (0-23):

Ejemplos

const d = new Date("2021-03-25");
d.getHours();

Pruébelo usted mismo →

<!DOCTYPE html>
<html>
<body>
<h1>JavaScript Dates</h1>
<h2>The getHours() Method</h2>
<p>Return the hours of a date as a number (0-23):</p>

<p id="demo"></p>

<script>
const d = new Date("2021-03-25");document.getElementById("demo").innerHTML = d.getHours();
</script>

</body>
</html>
const d = new Date();
d.getHours();

Pruébelo usted mismo →

<!DOCTYPE html>
<html>
<body>
<h1>JavaScript Dates</h1>
<h2>The getHours() Method</h2>
<p>Return the hours of a date as a number (0-23):</p>

<p id="demo"></p>

<script>
const d = new Date();
document.getElementById("demo").innerHTML = d.getHours();
</script>

</body>
</html>

El método getMinutes()

El método getMinutes() devuelve los minutos de una fecha como un número (0-59):

Ejemplos

const d = new Date("2021-03-25");
d.getMinutes();

Pruébelo usted mismo →

<!DOCTYPE html>
<html>
<body>
<h1>JavaScript Dates</h1>
<h2>The getMinutes() Method</h2>
<p>Returns the minutes of a date as a number (0-59):</p>

<p id="demo"></p>

<script>
const d = new Date("2021-03-25");
document.getElementById("demo").innerHTML = d.getMinutes();
</script>

</body>
</html>
const d = new Date();
d.getMinutes();

Pruébelo usted mismo →

<!DOCTYPE html>
<html>
<body>
<h1>JavaScript Dates</h1>
<h2>The getMinutes() Method</h2>
<p>Returns the minutes of a date as a number (0-59):</p>

<p id="demo"></p>

<script>
const d = new Date();
document.getElementById("demo").innerHTML = d.getMinutes();
</script>

</body>
</html>

El método getSeconds()

El método getSeconds() devuelve los segundos de una fecha como un número (0-59):

Ejemplos

const d = new Date("2021-03-25");
d.getSeconds();

Pruébelo usted mismo →

<!DOCTYPE html>
<html>
<body>
<h1>JavaScript Dates</h1>
<h2>The getSeconds() Method</h2>
<p>Return the seconds of a date as a number (0-59):</p>

<p id="demo"></p>

<script>
const d = new Date("2021-03-25");
document.getElementById("demo").innerHTML = d.getSeconds();
</script>

</body>
</html>
const d = new Date();
d.getSeconds();

Pruébelo usted mismo →

<!DOCTYPE html>
<html>
<body>
<h1>JavaScript Dates</h1>
<h2>The getSeconds() Method</h2>
<p>Return the seconds of a date as a number (0-59):</p>

<p id="demo"></p>

<script>
const d = new Date();
document.getElementById("demo").innerHTML = d.getSeconds();
</script>

</body>
</html>

El método getMillisegundos()

El método getMillisegundos() devuelve los milisegundos de una fecha como un número (0-999):

Ejemplos

const d = new Date("2021-03-25");
d.getMilliseconds();

Pruébelo usted mismo →

<!DOCTYPE html>
<html>
<body>
<h1>JavaScript Dates</h1>
<h2>The getMilliseconds() Method</h2>
<p>Return the milliseconds of a date as a number (0-999):</p>

<p id="demo"></p>

<script>
const d = new Date("2021-03-25");
document.getElementById("demo").innerHTML = d.getMilliseconds();
</script>

</body>
</html>
const d = new Date();
d.getMilliseconds();

Pruébelo usted mismo →

<!DOCTYPE html>
<html>
<body>
<h1>JavaScript Dates</h1>
<h2>The getMilliseconds() Method</h2>
<p>Return the milliseconds of a date as a number (0-999):</p>

<p id="demo"></p>

<script>
const d = new Date();
document.getElementById("demo").innerHTML = d.getMilliseconds();
</script>

</body>
</html>

El método getDay()

El método getDay() devuelve el día de la semana de una fecha como un número (0-6).

Nota

En JavaScript, el primer día de la semana (día 0) es el domingo.

Algunos países del mundo consideran que el primer día de la semana es el lunes.

Ejemplos

const d = new Date("2021-03-25");
d.getDay();

Pruébelo usted mismo →

<!DOCTYPE html>
<html>
<body>
<h1>JavaScript Dates</h1>
<h2>The getDay() Method</h2>
<p>Return the weekday as a number:</p>

<p id="demo"></p>

<script>
const d = new Date("2021-03-25");
document.getElementById("demo").innerHTML = d.getDay();
</script>

</body>
</html>
const d = new Date();
d.getDay();

Pruébelo usted mismo →

<!DOCTYPE html>
<html>
<body>
<h1>JavaScript Dates</h1>
<h2>The getDay() Method</h2>
<p>Return the weekday as a number:</p>

<p id="demo"></p>

<script>
const d = new Date();
document.getElementById("demo").innerHTML = d.getDay();
</script>

</body>
</html>

Nota

Puedes usar una variedad de nombres y getDay() para devolver el día de la semana como nombre:

Ejemplos

const days = ["Sunday", "Monday", "Tuesday", "Wednesday",
"Thursday", "Friday", "Saturday"];

const d = new Date("2021-03-25");
let day = days[d.getDay()];

Pruébelo usted mismo →

<!DOCTYPE html>
<html>
<body>
<h1>JavaScript Dates</h1>
<h2>The getDay() Method</h2>
<p>Return the weekday as a number.</p>
<p>You can use an array of names to return the weekday as a name:</p>

<p id="demo"></p>

<script>
const days = ["Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"];

const d = new Date("2021-03-25");
let day = days[d.getDay()];
document.getElementById("demo").innerHTML = day;
</script>

</body>
</html>
const days = ["Sunday", "Monday", "Tuesday", "Wednesday",
"Thursday", "Friday", "Saturday"];

const d = new Date();
let day = days[d.getDay()];

Pruébelo usted mismo →

<!DOCTYPE html>
<html>
<body>
<h1>JavaScript Dates</h1>
<h2>The getDay() Method</h2>
<p>Return the weekday as a number.</p>
<p>You can use an array of names to return the weekday as a name:</p>

<p id="demo"></p>

<script>
const days = ["Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"];

const d = new Date();
let day = days[d.getDay()];
document.getElementById("demo").innerHTML = day;
</script>

</body>
</html>

El método getTime()

El método getTime() devuelve el número de milisegundos desde el 1 de enero de 1970:

Ejemplos

const d = new Date("1970-01-01");
d.getTime();

Pruébelo usted mismo →

<!DOCTYPE html>
<html>
<body>
<h1>JavaScript Dates</h1>
<h2>The getTime() Method</h2>
<p></p>
<p>Return the number of milliseconds since midnight January 1, 1970:</p>

<p id="demo"></p>

<script>
const d = new Date("1970-01-01");
document.getElementById("demo").innerHTML = d.getTime();
</script>

</body>
</html>
const d = new Date("2021-03-25");
d.getTime();

Pruébelo usted mismo →

<!DOCTYPE html>
<html>
<body>
<h1>JavaScript Dates</h1>
<h2>The getTime() Method</h2>
<p></p>
<p>Return the number of milliseconds since midnight January 1, 1970:</p>

<p id="demo"></p>

<script>
const d = new Date("2021-03-25");
document.getElementById("demo").innerHTML = d.getTime();
</script>

</body>
</html>
const d = new Date();
d.getTime();

Pruébelo usted mismo →

<!DOCTYPE html>
<html>
<body>
<h1>JavaScript Dates</h1>
<h2>The getTime() Method</h2>
<p></p>
<p>Return the number of milliseconds since midnight January 1, 1970:</p>

<p id="demo"></p>

<script>
const d = new Date();
document.getElementById("demo").innerHTML = d.getTime();
</script>

</body>
</html>

El método Date.now()

Date.now() devuelve el número de milisegundos desde el 1 de enero de 1970.

Ejemplos

let ms = Date.now();

Pruébelo usted mismo →

<!DOCTYPE html>
<html>
<body>

<h1>JavaScript Dates</h1>
<h2>The Date.now() Method</h2>
<p>Return the current date/time in milliseconds since January 1, 1970:</p>

<p id="demo"></p>

<script>
const date = Date.now();
document.getElementById("demo").innerHTML = date;
</script>

</body>
</html>

Calcule el número de años desde el 01/01/1970:

const minute = 1000 * 60;
const hour = minute * 60;
const day = hour * 24;
const year = day * 365;

let years = Math.round(Date.now() / year);

Pruébelo usted mismo →

<!DOCTYPE html>
<html>
<body>

<h1>JavaScript Dates</h1>
<h2>Using Date.now()</h2>
<p>Calculate the number of years since January 1, 1970:</p>

<p id="demo"></p>

<script>
// Calculate milliseconds in a year
const minute = 1000 * 60;
const hour = minute * 60;
const day = hour * 24;
const year = day * 365;

// Divide Date.now() with a year
let years = Math.round(Date.now() / year);
document.getElementById("demo").innerHTML = years;
</script>

</body>
</html>

Date.now() es un método estático del objeto Date.

No puedes usarlo en un objeto de fecha como myDate.now().

La sintaxis es siempre Fecha.now().


Métodos de obtención de fecha UTC

getUTCDate() / getDate()

Devuelve la fecha UTC

getUTCFullYear() / getFullYear()

Devuelve el año UTC

getUTCMonth() / getMonth()

Devuelve el mes UTC

getUTCDay() / getDay()

Devuelve el día UTC

getUTCHours() / getHours()

Devuelve la hora UTC

getUTCMinutes() / getMinutes()

Devuelve los minutos UTC

getUTCSeconds() / getSeconds()

Devuelve los segundos UTC

getUTCMilliseconds() / getMilliseconds()

Devuelve los milisegundos UTC

UTC methods use UTC time (Coordinated Universal Time).

UTC time is the same as GMT (Greenwich Mean Time).

The difference between Local time and UTC time can be up to 24 hours.






El método getTimezoneOffset()

El método getTimezoneOffset() devuelve la diferencia (en minutos) entre la hora local y la hora UTC:

Ejemplo

let diff = d.getTimezoneOffset();

Pruébelo usted mismo →

<!DOCTYPE html>
<html>
<body>

<h1>JavaScript Dates</h1>
<h2>The getTimezoneOffset() Method</h2>

<p>The time zone difference in minutes is:</p>
<p id="demo"></p>

<script>
const d = new Date();
document.getElementById("demo").innerHTML = d.getTimezoneOffset();
</script>

</body>
</html>

Referencia completa de fecha de JavaScript

Para obtener una referencia completa de la fecha, visite nuestro:

Referencia completa de fechas de JavaScript.

La referencia contiene descripciones y ejemplos de todas las propiedades de fecha y métodos.