Nodejs Utililty Methods Date Convert

List of utility methods to do Date Convert

Description

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

Method

toDateOrTimeStr()
Date.prototype.toDateOrTimeStr = function() {
  if (this.isToday()){
    var dt = this.toLocaleTimeString();
    return dt.slice(0, -6) + dt.slice(-3); 
  } else
    return this.toLocaleDateString();
toDateStr()
function _pad(n, size){
  return ('00000' + n).slice(-size);
Date.prototype.toDateStr = function() {
  return this.getFullYear() 
    + '-' + _pad((this.getMonth()+1), 2)
    + '-' + _pad(this.getDate(), 2);
};
Date.prototype.toTimeStr = function() {
...
toDateTimeFormat()
Date.prototype.toDateTimeFormat = function() {
  var year = this.getFullYear().toString();
  var month = (this.getMonth() + 1).toString();
  var day = this.getDate().toString();
  var hour = this.getHours().toString();
  var minute = this.getMinutes().toString();
  var second = this.getSeconds().toString();
  return year + '-' + (month[1] ? month : '0'+month[0]) + '-'+ (day[1] ? day : '0'+day[0]) + ' ' + (hour[1] ? hour : '0'+hour[0]) + ':' + (minute[1] ? minute : '0'+minute[0]) + ':' + (second[1] ? second : '0'+second[0]);
toEndTimeInputValue(()
Date.prototype.toEndTimeInputValue = (function() {
  var local = new Date(this);
  local.setMinutes(this.getMinutes() - this.getTimezoneOffset() + 60);
  return local.toJSON().slice(11,13) + ":00:00";
});
toFixedDate()
Date.prototype.toFixedDate = function(){
  var months = [
    "January", "February", "March",
    "April", "May", "June",
    "Sol",
    "July", "August", "September",
    "October", "November", "December"
  ]
  var daysOfWeek = ["Sunday", "Monday", "Tuesday", "Wednesday",
...
toFormat(format)
Date.prototype.toFormat = function (format) {
  format = format.replace(/H/, this.getHours());
  format = format.replace(/m/, this.getMinutes());
  format = format.replace(/s/, this.getSeconds());
  return format;
};
toISO8601()
Date.prototype.toISO8601 = function() {
  function pad(n) {
    return n < 10 ? '0' + n : n;
  return this.getUTCFullYear() + '-' + pad(this.getUTCMonth() + 1) + '-' + pad(this.getUTCDate()) + 'T' + pad(this.getUTCHours())
   + ':' + pad(this.getUTCMinutes()) + ':' + pad(this.getUTCSeconds()) + 'Z';
};
toISO8601()
Date.prototype.toISO8601 = function() {    
    var s = [
          this.getFullYear()
        , Date.padZero(this.getMonth() + 1)
        , Date.padZero(this.getDate())
    ].join("-") + "T";
    s += this.getTimeString();
    s += this.getTimezoneOffsetString(true);
    return s;
...
toISO8601(key)
Date.prototype.toISO8601 = function (key) {
    function f(n) {
        return n < 10 ? '0' + n : n;
    function f2(n) {
        if(n < 10)
          return '00' + n;
        return n < 100 ? '0' + n : n;
    return isFinite(this.valueOf()) ?
           this.getUTCFullYear()   + '-' +
         f(this.getUTCMonth() + 1) + '-' +
         f(this.getUTCDate())      + 'T' +
         f(this.getUTCHours())     + ':' +
         f(this.getUTCMinutes())   + ':' +
         f(this.getUTCSeconds())   + '.' +
         f2(this.getUTCMilliseconds()) + '-0000' : null;
};
toInputFormat()
Date.prototype.toInputFormat = function() {
   var yyyy = this.getFullYear().toString();
   var mm = (this.getMonth()+1).toString(); 
   var dd  = this.getDate().toString();
   return yyyy +'-'+ (mm[1]?mm:"0"+mm[0]) +'-'+ (dd[1]?dd:"0"+dd[0]); 
  };