Nodejs Utililty Methods Date Get

List of utility methods to do Date Get

Description

The list of methods to do Date Get are organized into topic(s).

Method

compare(targetDate)
Date.prototype.compare = function(targetDate){
  if(this.getTime() == targetDate.getTime()) return 0;
  var result = (this.getTime() > targetDate.getTime()) ? 1 : -1;
  return result;
dateEquals(targetDate)
Date.prototype.dateEquals = function (targetDate) {
  let sourceDate = this;
  if (targetDate instanceof Date) {
    return (sourceDate.getDate() == targetDate.getDate()) && (sourceDate.getMonth() == targetDate.getMonth()) && (sourceDate.getFullYear() == targetDate.getFullYear());
  } else {
    return false;
};
getAge()
Date.prototype.getAge = function() {
    var today = new Date();
    var birthDate = this;
    var age = today.getFullYear() - birthDate.getFullYear();
    var m = today.getMonth() - birthDate.getMonth();
    if (m < 0 || (m === 0 && today.getDate() < birthDate.getDate())) {
        age--;
    return age;
...
getAtom()
Date.prototype.getAtom = function()
  return   this.getFullYear() + '-' + 
    this.getMonthZero() + '-' + 
    this.getDateZero() + 'T' + 
    this.getHoursZero() + ':' + 
    this.getMinutesZero() + ':' + 
    this.getSecondsZero() + 'Z';
};
...
getCode()
Date.prototype.getCode = function(){
    var start = new Date(this.getFullYear(), 0, 0);
    var diff = this - start;
    var oneDay = 1000 * 60 * 60 * 24;
    var day = Math.floor(diff / oneDay);
    return ( 24 * day ) + this.getHours();
};
getDST()
Date.prototype.getDST = function() {
    return this.getTimezoneOffset() - this.getStdTimezone();
getDate2()
Date.prototype.getDate2 = function () {
   var date = this.getDate();
   return (date < 10 ? '0' : '') + date;
};
getDateDiff(difference)
function getDateDiff(difference) {
  var ret    = {};
  ret.days  = Math.floor(difference / 1000 / 60 / 60 / 24);
  ret.hours  = Math.floor(difference / 1000 / 60 / 60 - (24 * ret.days));
  ret.mins  = Math.floor(difference / 1000 / 60 - (24 * 60 * ret.days) - (60 * ret.hours));
  ret.secs  = Math.floor(difference / 1000 - (24 * 60 * 60 * ret.days) - (60 * 60 * ret.hours) - (60 * ret.mins));
  return ret;
getDateDifference(otherDate)
Date.prototype.getDateDifference = function(otherDate){
  var oneDay = 1000*3600*24;
  var d = new Date(+this);
  d.setHours(0,0,0,0);
  otherDate.setHours(0,0,0,0);
  var timeDiff = d.getTime() - otherDate.getTime();
  return Math.floor(timeDiff/oneDay);
getDateOffset(target)
Date.prototype.getDateOffset = function(target) {
    var milliSeconds = target.getTime() - this.getTime();
    return Math.ceil(milliSeconds/1000/60/60/24);
};