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.
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>
Obtener año como un número de cuatro dígitos (aaaa)
Obtener mes como un número (0-11)
Obtener día como un número (1-31)
Obtener día de la semana como un número (0-6)
Obtener hora (0-23)
Obtener minuto (0-59)
Consigue el segundo (0-59)
Obtener milisegundo (0-999)
Obtener tiempo (milisegundos desde el 1 de enero de 1970)
Los métodos de obtención anteriores devuelven hora local.
La hora universal (UTC) está documentada en la parte inferior de esta página.
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.
getFullYear()
El método getFullYear()
devuelve el año de una fecha como un número de cuatro dígitos:
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>
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!
getMonth()
El método getMonth()
devuelve el mes de una fecha como un número (0-11).
En JavaScript, enero es el mes número 0, febrero es el número 1,...
Finalmente, diciembre es el mes número 11.
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>
Puede utilizar una serie de nombres para devolver el mes como nombre:
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>
getDate()
El método getDate()
devuelve el día de una fecha como un número (1-31):
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>
getHours()
El método getHours()
devuelve las horas de una fecha como un número (0-23):
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>
getMinutes()
El método getMinutes()
devuelve los minutos de una fecha como un número (0-59):
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>
getSeconds()
El método getSeconds()
devuelve los segundos de una fecha como un número (0-59):
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>
getMillisegundos()
El método getMillisegundos()
devuelve los milisegundos de una fecha como un número (0-999):
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>
getDay()
El método getDay()
devuelve el día de la semana de una fecha como un número (0-6).
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.
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>
Puedes usar una variedad de nombres y getDay()
para devolver el día de la semana como nombre:
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>
getTime()
El método getTime()
devuelve el número de milisegundos desde el 1 de enero de 1970:
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>
Date.now()
Date.now()
devuelve el número de milisegundos desde el 1 de enero de 1970.
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()
.
Devuelve la fecha UTC
Devuelve el año UTC
Devuelve el mes UTC
Devuelve el día UTC
Devuelve la hora UTC
Devuelve los minutos UTC
Devuelve los segundos UTC
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.
getTimezoneOffset()
El método getTimezoneOffset()
devuelve la diferencia (en minutos) entre la hora local y la hora UTC:
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>
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.