// Compute diff datetime
// Don't know why fetch from MySQL query become Date JS object without convert.
var last_activity = result_find_usr_by_id[0].updated_at;
console.log('last ' + last_activity);
var oneDay = 24*60*60*1000; // hours*minutes*seconds*milliseconds
var todayDate = new Date();
console.log('today ' + todayDate);
var diffDays = 0;
if(typeof last_activity !== 'undefined') {
// Split timestamp into [ Y, M, D, h, m, s ]
// var t = last_activity.split(/[- :]/);
// Apply each element to the Date function
// var last_activity = new Date(Date.UTC(t[0], t[1]-1, t[2], t[3], t[4], t[5]));
var diffDays = Math.round(Math.abs((last_activity.getTime() - todayDate.getTime())/(oneDay)));
console.log('diff '+ diffDays);
}
/**
* You first need to create a formatting function to pad numbers to two digits…
**/
function twoDigits(d) {
if(0 <= d && d < 10) return "0" + d.toString();
if(-10 < d && d < 0) return "-0" + (-1*d).toString();
return d.toString();
}
/**
* …and then create the method to output the date string as desired.
* Some people hate using prototypes this way, but if you are going
* to apply this to more than one Date object, having it as a prototype
* makes sense.
**/
Date.prototype.toMysqlFormat = function() {
return this.getUTCFullYear() + "-" + twoDigits(1 + this.getUTCMonth()) + "-" + twoDigits(this.getUTCDate()) + " " + twoDigits(this.getUTCHours()) + ":" + twoDigits(this.getUTCMinutes()) + ":" + twoDigits(this.getUTCSeconds());
};
// Don't know why fetch from MySQL query become Date JS object without convert.
var last_activity = result_find_usr_by_id[0].updated_at;
console.log('last ' + last_activity);
var oneDay = 24*60*60*1000; // hours*minutes*seconds*milliseconds
var todayDate = new Date();
console.log('today ' + todayDate);
var diffDays = 0;
if(typeof last_activity !== 'undefined') {
// Split timestamp into [ Y, M, D, h, m, s ]
// var t = last_activity.split(/[- :]/);
// Apply each element to the Date function
// var last_activity = new Date(Date.UTC(t[0], t[1]-1, t[2], t[3], t[4], t[5]));
var diffDays = Math.round(Math.abs((last_activity.getTime() - todayDate.getTime())/(oneDay)));
console.log('diff '+ diffDays);
}
/**
* You first need to create a formatting function to pad numbers to two digits…
**/
function twoDigits(d) {
if(0 <= d && d < 10) return "0" + d.toString();
if(-10 < d && d < 0) return "-0" + (-1*d).toString();
return d.toString();
}
/**
* …and then create the method to output the date string as desired.
* Some people hate using prototypes this way, but if you are going
* to apply this to more than one Date object, having it as a prototype
* makes sense.
**/
Date.prototype.toMysqlFormat = function() {
return this.getUTCFullYear() + "-" + twoDigits(1 + this.getUTCMonth()) + "-" + twoDigits(this.getUTCDate()) + " " + twoDigits(this.getUTCHours()) + ":" + twoDigits(this.getUTCMinutes()) + ":" + twoDigits(this.getUTCSeconds());
};
Comments
Post a Comment