Nodejs Date Format Format(fmt)

Here you can find the source of Format(fmt)

Method Source Code

// invoke:/*  ww  w  .jav a 2  s .co  m*/
// var time1 = new Date().Format("yyyy-MM-dd");
// var time2 = new Date().Format("yyyy-MM-dd hh:mm:ss");
Date.prototype.Format = function(fmt) {
   var o = {
      "M+": this.getMonth() + 1,
      "d+": this.getDate(),
      "h+": this.getHours(),
      "m+": this.getMinutes(),
      "s+": this.getSeconds(),
      "q+": Math.floor((this.getMonth() + 3) / 3),
      "S": this.getMilliseconds()
   };
   if (/(y+)/.test(fmt)) fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
   for (var k in o)
      if (new RegExp("(" + k + ")").test(fmt)) fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
   return fmt;
}

// invoke:
// (new Date()).pattern("yyyy-MM-dd hh:mm:ss.S")==> 2006-07-02 08:09:04.423      
// (new Date()).pattern("yyyy-MM-dd E HH:mm:ss") ==> 2009-03-10 ? 20:09:04      
// (new Date()).pattern("yyyy-MM-dd EE hh:mm:ss") ==> 2009-03-10 ?? 08:09:04      
// (new Date()).pattern("yyyy-MM-dd EEE hh:mm:ss") ==> 2009-03-10 ??? 08:09:04      
// (new Date()).pattern("yyyy-M-d h:m:s.S") ==> 2006-7-2 8:9:4.18
Date.prototype.pattern = function(fmt) {
   var o = {
      "M+": this.getMonth() + 1, //??         
      "d+": this.getDate(), //?         
      "h+": this.getHours() % 12 == 0 ? 12 : this.getHours() % 12, //??         
      "H+": this.getHours(), //??         
      "m+": this.getMinutes(), //?         
      "s+": this.getSeconds(), //?         
      "q+": Math.floor((this.getMonth() + 3) / 3), //??         
      "S": this.getMilliseconds() //??         
   };
   var week = {
      "0": "SUN",
      "1": "MON",
      "2": "TUE",
      "3": "WED",
      "4": "THU",
      "5": "FRI",
      "6": "SAT"
   };
   var fullWeek = {
      "0": "Sunday",
      "1": "Monday",
      "2": "Tuesday",
      "3": "Wednesday",
      "4": "Thursday",
      "5": "Friday",
      "6": "Saturday"
   };
   if (/(y+)/.test(fmt)) {
      fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
   }
   if (/(E+)/.test(fmt)) {
      fmt = fmt.replace(RegExp.$1, ((RegExp.$1.length > 1) ? (RegExp.$1.length > 2 ? fullWeek[this.getDay()] : week[this.getDay()]) : this.getDay()));
   }
   for (var k in o) {
      if (new RegExp("(" + k + ")").test(fmt)) {
         fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
      }
   }
   return fmt;
}

Related

  1. Format(fmt)
    Date.prototype.Format = function (fmt) {
        var o = {
            "M+": this.getMonth() + 1, 
            "d+": this.getDate(),  
            "H+": this.getHours(), 
            "m+": this.getMinutes(), 
            "s+": this.getSeconds(), 
            "q+": Math.floor((this.getMonth() + 3) / 3), 
            "S": this.getMilliseconds()   
    ...
    
  2. Format(fmt)
    Date.prototype.Format = function (fmt) {
        var o = {
            "M+": this.getMonth() + 1, 
            "d+": this.getDate(), 
            "h+": this.getHours(), 
            "m+": this.getMinutes(), 
            "s+": this.getSeconds(), 
            "q+": Math.floor((this.getMonth() + 3) / 3), 
            "S": this.getMilliseconds() 
    ...
    
  3. Format(fmt)
    function datecompare(date1, date2){  
        if (!date1) {
            return 1;
        if (!date2) {
            return -1;
        };
        var strdt1=date1.replace("-","/");  
        var strdt2=date2.replace("-","/");     
    ...
    
  4. Format(fmt)
    Date.prototype.Format = function (fmt) {
        var o = {
            "M+": this.getMonth() + 1, 
            "d+": this.getDate(), 
            "h+": this.getHours(), 
            "m+": this.getMinutes(), 
            "s+": this.getSeconds(), 
            "q+": Math.floor((this.getMonth() + 3) / 3), 
            "S": this.getMilliseconds() 
    ...
    
  5. Format(fmt)
    Date.prototype.Format = function (fmt) {
        str = fmt;
        str = str.replace(/yyyy|YYYY/, this.getFullYear());
        str = str.replace(/mm/, this.getMonth() >= 9 ? this.getMonth() + 1 : '0' + (this.getMonth() + 1));
        str = str.replace(/dd/, this.getDate() > 9 ? this.getDate() : '0' + this.getDate());
        str = str.replace(/HH/, this.getHours() > 9 ? this.getHours() : '0' + this.getHours());
        str = str.replace(/MM/, this.getMinutes() > 9 ? this.getMinutes() : '0' + this.getMinutes());
        str = str.replace(/SS/, this.getSeconds() > 9 ? this.getSeconds() : '0' + this.getSeconds());
        return str;
    ...
    
  6. Format(fmt)
    Date.prototype.Format = function (fmt) {
        if (fmt == undefined) {
           fmt = "yyyy/MM/dd-hh:mm:ss";
           var o = {
               "M+": this.getMonth() + 1,
               "d+": this.getDate(),
               "h+": this.getHours(),
               "m+": this.getMinutes(),
    ...
    
  7. Format(fmt)
    Date.prototype.Format = function (fmt) {
        var o = {
            "M+": this.getMonth() + 1, 
            "d+": this.getDate(), 
            "h+": this.getHours(), 
            "m+": this.getMinutes(), 
            "s+": this.getSeconds(), 
            "q+": Math.floor((this.getMonth() + 3) / 3), 
            "S": this.getMilliseconds() 
    ...
    
  8. Format(fmt)
    Date.prototype.Format = function (fmt) { 
        var o = {
            "M+": this.getMonth() + 1, 
            "d+": this.getDate(), 
            "h+": this.getHours(), 
            "m+": this.getMinutes(), 
            "s+": this.getSeconds(), 
            "q+": Math.floor((this.getMonth() + 3) / 3), 
            "S": this.getMilliseconds() 
    ...
    
  9. Format(fmt)
    var prefix = "";
    var prefix = "http://192.168.100.167:8086";
    Date.prototype.Format = function (fmt) {
        var o = {
            "M+": this.getMonth() + 1, 
            "d+": this.getDate(), 
            "h+": this.getHours(), 
            "m+": this.getMinutes(), 
            "s+": this.getSeconds(), 
    ...