.getMonth()devuelve un número basado en cero, por lo que para obtener el mes correcto, debe agregar 1, por lo que llamar .getMonth()puede devolver 4y no 5.
Entonces, en su código, podemos usar currentdate.getMonth()+1para generar el valor correcto. Adicionalmente:
.getDate()devuelve el día del mes <- este es el que desea
.getDay()es un método separado del Dateobjeto que devolverá un número entero que representa el día actual de la semana (0-6), 0 == Sundayetc.
entonces su código debería verse así:
var currentdate = new Date();
var datetime = "Last Sync: " + currentdate.getDate() + "/"
+ (currentdate.getMonth()+1) + "/"
+ currentdate.getFullYear() + " @ "
+ currentdate.getHours() + ":"
+ currentdate.getMinutes() + ":"
+ currentdate.getSeconds();
Las instancias de fecha de JavaScript heredan de Date.prototype. Puede modificar el objeto prototipo del constructor para afectar las propiedades y los métodos heredados por las instancias de JavaScript Date
Puede utilizar el Dateobjeto prototipo para crear un nuevo método que devolverá la fecha y hora de hoy. Estos nuevos métodos o propiedades serán heredados por todas las instancias del Dateobjeto, lo que lo hace especialmente útil si necesita reutilizar esta funcionalidad.
// For todays date;
Date.prototype.today = function () {
return ((this.getDate() < 10)?"0":"") + this.getDate() +"/"+(((this.getMonth()+1) < 10)?"0":"") + (this.getMonth()+1) +"/"+ this.getFullYear();
}
// For the time now
Date.prototype.timeNow = function () {
return ((this.getHours() < 10)?"0":"") + this.getHours() +":"+ ((this.getMinutes() < 10)?"0":"") + this.getMinutes() +":"+ ((this.getSeconds() < 10)?"0":"") + this.getSeconds();
}
Luego, simplemente puede recuperar la fecha y la hora haciendo lo siguiente:
var newDate = new Date();
var datetime = "LastSync: " + newDate.today() + " @ " + newDate.timeNow();
O llame al método en línea para que simplemente sea:
var datetime = "LastSync: " + new Date().today() + " @ " + new Date().timeNow();