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

toRelativeTime()
Date.prototype.toRelativeTime = function() {
  var delta       = new Date() - this;
  var units       = null;
  var conversions = {
    millisecond: 1, 
    second: 1000,   
    minute: 60,     
    hour:   60,     
    day:    24,     
...
toRelativeTime(now_threshold)
Date.prototype.toRelativeTime = function(now_threshold) {
  var delta = new Date() - this;
  now_threshold = parseInt(now_threshold, 10);
  if (isNaN(now_threshold)) {
    now_threshold = 0;
  if (delta <= now_threshold) {
    return 'Just now';
  var units = null;
  var conversions = {
    millisecond: 1, 
    second: 1000,   
    minute: 60,     
    hour:   60,     
    day:    24,     
    month:  30,     
    year:   12      
  };
  for (var key in conversions) {
    if (delta < conversions[key]) {
      break;
    } else {
      units = key; 
      delta = delta / conversions[key];
  delta = Math.floor(delta);
  if (delta !== 1) { units += "s"; }
  return [delta, units, "ago"].join(" ");
};
toRelativeTime(now_threshold)
Date.prototype.toRelativeTime = function(now_threshold) {
  var delta = new Date() - this;
  now_threshold = parseInt(now_threshold, 10);
  if (isNaN(now_threshold)) {
    now_threshold = 0;
  if (Math.abs(delta) <= now_threshold) {
    return 'Just now';
  var units = null;
  var conversions = {
    millisecond: 1, 
    second: 1000,   
    minute: 60,     
    hour:   60,     
    day:    24,     
    month:  30,     
    year:   12      
  };
  for (var key in conversions) {
    if (Math.abs(delta) < conversions[key]) {
      break;
    } else {
      units = key; 
      delta = delta / conversions[key];
  delta = delta > 0 ? Math.floor(delta) : Math.ceil(delta);
  if (Math.abs(delta) !== 1) { units += "s"; }
  if (delta > 0) {
    return [delta, units, "ago"].join(" ");
  } else {
    return ["in", -delta, units].join(" ");
};
Date.fromString = function(str) {
  return new Date(Date.parse(str));
};
toShortTime()
Date.prototype.toShortTime = function() {
    return ('0' + this.getHours()).slice(-2) + ':' + ('0' + this.getMinutes()).slice(-2);
toSource()
Date.prototype.toSource = function(){
  return '(new Date(' + this.valueOf() + '))';
};
toStartTimeInputValue(()
Date.prototype.toStartTimeInputValue = (function() {
  var local = new Date(this);
  local.setMinutes(this.getMinutes() - this.getTimezoneOffset());
  return local.toJSON().slice(11,13) + ":00:00";
});
toText()
Date.prototype.toText=function(){ 
  var m; 
  var d; 
  if(this.getMonth()<9){ 
  m="0"+(this.getMonth()+1); 
  }else{ 
  m=this.getMonth()+1; 
  if(this.getDate()<10){ 
...
toTime()
Date.prototype.toTime = function () {
    var hours = this.getHours();
    var minutes = this.getMinutes();
    if (minutes < 10)
        minutes = "0" + minutes;
    if (hours < 10)
        hours = "0" + hours;
    return hours + ':' + minutes;
};
...
toTimeInputValue(()
Date.prototype.toTimeInputValue = (function() {
  var local = new Date(this);
  local.setMinutes(this.getMinutes() - this.getTimezoneOffset());
  return local.toJSON().slice(11,19);
});
toTurkishFormatDate(format)
Date.prototype.toTurkishFormatDate = function(format) {
    var date = this,
            day = date.getDate(),
            weekDay = date.getDay(),
            month = date.getMonth() + 1,
            year = date.getFullYear(),
            hours = date.getHours(),
            minutes = date.getMinutes(),
            seconds = date.getSeconds();
...