Return to Snippet

Revision: 59303
at August 31, 2012 17:34 by samsonl


Updated Code
function DateFmt() {
      this.dateMarkers = { 
	     d:['getDate',function(v) { return ("0"+v).substr(-2,2)}], 
             m:['getMonth',function(v) { return ("0"+v).substr(-2,2)}],
             n:['getMonth',function(v) {
                 var mthNames = ["Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"];
                 return mthNames[v];
	             }],
             w:['getDay',function(v) {
                 var dayNames = ["Sun","Mon","Tue","Wed","Thu","Fri","Sat"];
                 return dayNames[v];
	             }],
             y:['getFullYear'],
             H:['getHours',function(v) { return ("0"+v).substr(-2,2)}],
             M:['getMinutes',function(v) { return ("0"+v).substr(-2,2)}],
             S:['getSeconds',function(v) { return ("0"+v).substr(-2,2)}],
             i:['toISOString',null]
      };
     
      this.format = function(date, fmt) {
        var dateMarkers = this.dateMarkers
        var dateTxt = fmt.replace(/%(.)/g, function(m, p){
        var rv = date[(dateMarkers[p])[0]]()
      
        if ( dateMarkers[p][1] != null ) rv = dateMarkers[p][1](rv)

        return rv
      });
  	    
      return dateTxt
      }
    }
    
    fmt = new DateFmt()
    v = fmt.format(new Date(),"%w %d:%n:%y - %H:%M:%S %i")

Revision: 59302
at August 31, 2012 17:32 by samsonl


Updated Code
function DateFmt() {
      this.dateMarkers = { 
	     d:['getDate',function(v) { return ("0"+v).substr(-2,2)}], 
             m:['getMonth',function(v) { return ("0"+v).substr(-2,2)}],
             n:['getMonth',function(v) {
                 var mthNames = ["Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"];
                 return mthNames[v];
	             }],
             w:['getDay',function(v) {
                 var dayNames = ["Sun","Mon","Tue","Wed","Thu","Fri","Sat"];
                 return dayNames[v];
	             }],
             y:['getFullYear'],
             H:['getHours',function(v) { return ("0"+v).substr(-2,2)}],
             M:['getMinutes',function(v) { return ("0"+v).substr(-2,2)}],
             S:['getSeconds',function(v) { return ("0"+v).substr(-2,2)}],
             i:['toISOString',null]
      };
     
      this.format = function(date, fmt) {
	    var dateMarkers = this.dateMarkers
        var dateTxt = fmt.replace(/%(.)/g, function(m, p){
          var rv = date[(dateMarkers[p])[0]]()
      
          if ( dateMarkers[p][1] != null ) rv = dateMarkers[p][1](rv)

          return rv
  	    });
  	    
  	    return dateTxt
      }
    }
    
    fmt = new DateFmt()
    v = fmt.format(new Date(),"%w %d:%n:%y - %H:%M:%S %i")

Revision: 59301
at August 31, 2012 17:29 by samsonl


Initial Code
function DateFmt() {
      this.dateMarkers = { 
	         d:['getDate',function(v) { return ("0"+v).substr(-2,2)}], 
             m:['getMonth',function(v) { return ("0"+v).substr(-2,2)}],
             n:['getMonth',function(v) {
                 var mthNames = ["Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"];
                 return mthNames[v];
	             }],
             w:['getDay',function(v) {
                 var mthNames = ["Sun","Mon","Tue","Wed","Thu","Fri","Sat"];
                 return mthNames[v];
	             }],
             y:['getFullYear'],
             H:['getHours',function(v) { return ("0"+v).substr(-2,2)}],
             M:['getMinutes',function(v) { return ("0"+v).substr(-2,2)}],
             S:['getSeconds',function(v) { return ("0"+v).substr(-2,2)}],
             i:['toISOString',null]
      };
     
      this.format = function(date, fmt) {
	    var dateMarkers = this.dateMarkers
        var dateTxt = fmt.replace(/%(.)/g, function(m, p){
          var rv = date[(dateMarkers[p])[0]]()
      
          if ( dateMarkers[p][1] != null ) rv = dateMarkers[p][1](rv)

          return rv
  	    });
  	    
  	    return dateTxt
      }
    }
    
    fmt = new DateFmt()
    v = fmt.format(new Date(),"%w %d:%n:%y - %H:%M:%S %i")

Initial URL

                                

Initial Description
Javascript Basic Date Formatter

Initial Title
Javascript Basic Date Formatter

Initial Tags
date, format

Initial Language
JavaScript