Nodejs Date Format format(formatStr)

Here you can find the source of format(formatStr)

Method Source Code

Date.prototype.format= function(formatStr) 
{
var date = this;/* ww w .  j  a  v  a2s. com*/
var timeValues = function(){};
timeValues.prototype = {
   year:function(){
    if(formatStr.indexOf("yyyy")>=0)
    {
     return date.getYear();
    }
    else
    {
     return date.getYear().toString().substr(2);
    }
   },
   elseTime:function(val,formatVal){
    return formatVal>=0?(val<10?"0"+val:val):(val);
   },
   elseTime2:function(val,formatVal){
    return formatVal>=0?(val<100?(val<10?"00"+val:"0"+val):val):(val);
   },
   month:function(){
    return this.elseTime(date.getMonth ()+1,formatStr.indexOf("MM"));
   },
   day:function(){
    return this.elseTime(date.getDate(),formatStr.indexOf ("dd"));
   },
   hour:function(){
    return this.elseTime(date.getHours(),formatStr.indexOf ("hh"));
   },
   minute:function(){
    return this.elseTime(date.getMinutes (),formatStr.indexOf("mm"));
   },
   second:function(){
    return this.elseTime(date.getSeconds(),formatStr.indexOf ("ss"));
   },
   millisecond:function(){
    return this.elseTime2(date.getMilliseconds(),formatStr.indexOf ("lll"));
   }
}
var tV = new timeValues();
var replaceStr = {
   year:["yyyy","yy"],
   month:["MM","M"],
   day:["dd","d"],
   hour:["hh","h"],
   minute:["mm","m"],
   second:["ss","s"],
   millisecond:["lll","l"]
};
for(var key in replaceStr)
{
   formatStr = formatStr.replace(replaceStr[key][0],eval ("tV."+key+"()"));
   formatStr = formatStr.replace(replaceStr[key][1],eval ("tV."+key+"()"));
}
return formatStr;
}

function CheckDateTime(str)  
{   
    var reg = /^(\d+)-(\d{ 1,2 })-(\d{ 1,2 }) (\d{ 1,2 }):(\d{ 1,2 }):(\d{ 1,2 })$/;   
    var r = str.match(reg);   
    if(r==null)return false;   
    r[2]=r[2]-1;   
    var d= new Date(r[1],r[2],r[3],r[4],r[5],r[6]);   
    if(d.getFullYear()!=r[1])return false;   
    if(d.getMonth()!=r[2])return false;   
    if(d.getDate()!=r[3])return false;   
    if(d.getHours()!=r[4])return false;   
    if(d.getMinutes()!=r[5])return false;   
    if(d.getSeconds()!=r[6])return false;   
    return true;   
}

Related

  1. format(format,date)
    Date.prototype.format = function(format,date){
        if(!format || typeof format !== 'string'){
        throw new Error('format is undefiend or type is Error');
      date = date instanceof Date? date : (typeof date === 'number'|| typeof date === 'string')? new Date(date): new Date();
      var formatReg = {
        'y+': date.getFullYear(),
        'M+': date.getMonth()+1,
        'd+': date.getDate(),
    ...
    
  2. format(format,value)
    Date.prototype.format = function (format,value) {
      if (!format) {
        format = "yyyy-MM-dd hh:mm:ss";
      if(value==''||value==null){
        return '';
      var strdata=value.replace(/-/g,"/");
      var index=strdata.indexOf(".");
    ...
    
  3. format(format,value)
    Date.prototype.format = function (format,value) {
      if (!format) {
        format = "yyyy-MM-dd hh:mm:ss";
      if(value==''||value==null){
        return '';
      var strdata=value.replace(/-/g,"/");
      var index=strdata.indexOf(".");
    ...
    
  4. format(formatStr)
    Date.prototype.format = function(formatStr)   
        var str = formatStr;   
        var Week = ['?','?','?','?','?','?','?'];  
        str=str.replace(/yyyy|YYYY/,this.getFullYear());   
        str=str.replace(/yy|YY/,(this.getYear() % 100)>9?(this.getYear() % 100).toString():'0' + (this.getYear() % 100));   
        str=str.replace(/MM/,this.getMonth()>9?this.getMonth().toString():'0' + this.getMonth());   
        str=str.replace(/M/g,this.getMonth());   
        str=str.replace(/w|W/g,Week[this.getDay()]);   
    ...
    
  5. format(formatStr)
    Date.prototype.format = function(formatStr) {
        var str = formatStr;
        var Week = ['?','?','?','?','?','?','?'];
        str = str.replace(/yyyy|YYYY/,this.getFullYear());
        str = str.replace(/yy|YY/,(this.getYear() % 100)>9?(this.getYear() % 100).toString():'0' + (this.getYear() % 100));
        str = str.replace(/MM/,this.getMonth()>9?this.getMonth().toString():'0' + this.getMonth());
        str = str.replace(/M/g,this.getMonth());
        str = str.replace(/w|W/g,Week[this.getDay()]);
        str = str.replace(/dd|DD/,this.getDate()>9?this.getDate().toString():'0' + this.getDate());
    ...
    
  6. format(pattern)
    Date.prototype.format = function(pattern) {
      var returnValue = pattern;
      var format = {
        "y+": this.getFullYear(),
        "M+": this.getMonth() + 1,
        "d+": this.getDate(),
        "H+": this.getHours(),
        "m+": this.getMinutes(),
        "s+": this.getSeconds(),
    ...
    
  7. format(pattern)
    Date.prototype.format = function(pattern)
      if(!pattern){
        pattern = "yyyy-MM-dd";
      var o =
        "M+" : this.getMonth() + 1, 
        "d+" : this.getDate(), 
    ...
    
  8. format(pattern)
    Date.prototype.format = function(pattern){
        var pattern = pattern;    
        var dateObj = {
            "Y" : this.getFullYear(),
            "M" : this.getMonth()+1,
            "D" : this.getDate(),
            "h" : this.getHours(),
            "m" : this.getMinutes(),
            "s" : this.getSeconds()
    ...
    
  9. format(pattern, gmt)
    Date.prototype.format = function(pattern, gmt) {
      var result = '';
      var date = this;
      if(gmt != undefined) {
        var utc = date.getTime() + (date.getTimezoneOffset() * 60000);
        date = new Date(utc + (3600000 * gmt));
      for (var i = 0; i < pattern.length; i++) {
        var c = pattern[i];
    ...