Skip to content

Instantly share code, notes, and snippets.

@DavidPeralvarez
Created April 14, 2020 13:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save DavidPeralvarez/3a0b79403c675f2823325f96857f6ffd to your computer and use it in GitHub Desktop.
Save DavidPeralvarez/3a0b79403c675f2823325f96857f6ffd to your computer and use it in GitHub Desktop.
JavaScript - Trabajando con fechas
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Curso de Javascript</title>
</head>
<body>
<script src="scripts.js"></script>
</body>
</html>
/* Objetos Date */
// Tiempo en segundos que dura una tarea
// var inicio = new Date();
// for ( var i = 0; i < 10000; i++ ) {
// console.log( 'Tarea' );
// }
// var fin = new Date(),
// duracion = (fin.getTime() - inicio.getTime()) / 1000;
// console.log( duracion.toFixed(2) + ' segundos.' );
var currentDate = new Date();
console.log( currentDate );
// sumar años
Date.prototype.sumYears = function( years ) {
this.setYear( this.getFullYear() + years );
return this;
}
currentDate.sumYears( -5 );
console.log( currentDate );
// sumar meses
Date.prototype.sumMonths = function( months ) {
this.setMonth( this.getMonth() + months );
return this;
}
currentDate.sumMonths( 2 );
console.log( currentDate );
// sumar días
Date.prototype.sumDays = function( days ) {
this.setDate( this.getDate() + days );
return this;
}
currentDate.sumDays( 30 );
console.log( currentDate );
// sumar horas, minutos, segundos, milisegundos
// setHours, setMinutes, setSeconds, setMilliseconds
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment