Tengo un script que me gusta comprobar cuándo se ejecutó por última vez. Por lo tanto, registro la fecha y hora de la "última ejecución" en una hoja antes de que finalice el script y extraigo los datos cada vez que se inicia el script. Sin embargo, el cálculo siempre es incorrecto cuando comparo la fecha actual con la última fecha de ejecución.
var dtToday = new Date(); var dtDebug=new Date(shtYahooDataRef.getRange(2,2).getValue()); //This is extra code for my debugging var dtLastUpdate=new Date(shtYahooDataRef.getRange(1,2).getValue()); //This is where I retrieve the last ran datetime // Just to check that the dates are correct Logger.log (Utilities.formatDate(dtToday, Session.getScriptTimeZone(), 'MMMM dd, yyyy hh:mm:ss Z')); Logger.log (Utilities.formatDate(dtDebug, Session.getScriptTimeZone(), 'MMMM dd, yyyy hh:mm:ss Z')); Logger.log (Utilities.formatDate(dtLastUpdate, Session.getScriptTimeZone(), 'MMMM dd, yyyy hh:mm:ss Z')); //Given that dtToday and dtDebug are very close, I expect the below to be very close but this is not case. Hence I wonder why Logger.log (dtToday-dtLastUpdate); Logger.log (dtDebug-dtLastUpdate);Aquí está el registro
December 07, 2021 02:26:25 +0800 December 07, 2021 02:26:13 +0800 December 07, 2021 02:25:13 +0800 4.3272544E7 60000.0 ¿Cómo es que dtToday-dtLastUpdate y dtDebug-dtLastUpdate pueden ser tan diferentes? ¿No dtToday-dtLastUpdate en el estadio de béisbol de 60000?
Información adicional. Este es el código real donde salgo del script si la última vez que se ejecutó fue hace menos de 6 horas. La condición siempre era falsa.
var t1 = dtToday.getTime(), t2 = dtLastUpdate.getTime(); if ( Math.floor((t1-t2)/(3600*1000))< 6) { return; }; // 3600*1000 is milliseconds in an hour Update only is last update was 6 hr agoComentario adicional: si tengo un script simple
var dtToday = new Date(); Logger.log (Utilities.formatDate(dtToday, "Asia/Hong_Kong", 'MMMM dd, yyyy hh:mm:ss Z')+" "+Session.getScriptTimeZone());y la salida es
1:57:21 PM Info December 10, 2021 01:57:21 +0800 Asia/Hong_Kong¿Cómo es que ejecutar el script a la 1:57 p. m. devuelve 01:57:21 +0800? ¿No debería ser 13:57:21 +0800?
Encontré el problema. Registré mal la hora.
En lugar de usar 'MMMM dd, yyyy hh:mm:ss Z' , debería haber usado 'MMMM dd, yyyy HH:mm:ss Z'
No puede "menos" directamente una fecha de otra fecha.
Primero tienes que convertir la fecha en milisegundos. Luego puede restar una fecha de otra y luego debe convertir la diferencia en minutos u horas.
var dtToday = new Date(); var dtDebug=new Date(shtYahooDataRef.getRange(2,2).getValue()); //This is extra code for my debugging var dtLastUpdate=new Date(shtYahooDataRef.getRange(1,2).getValue()); //This is where I retrieve the last ran datetime // Just to check that the dates are correct Logger.log (Utilities.formatDate(dtToday, Session.getScriptTimeZone(), 'MMMM dd, yyyy hh:mm:ss Z')); Logger.log (Utilities.formatDate(dtDebug, Session.getScriptTimeZone(), 'MMMM dd, yyyy hh:mm:ss Z')); Logger.log (Utilities.formatDate(dtLastUpdate, Session.getScriptTimeZone(), 'MMMM dd, yyyy hh:mm:ss Z')); //Given that dtToday and dtDebug are very close, I expect the below to be very close but this is not case. Hence I wonder why Logger.log (dtToday.valueOf()-dtLastUpdate.valueOf()); Logger.log (dtDebug.valueOf()-dtLastUpdate.valueOf());