Nodejs Date Format format(format)

Here you can find the source of format(format)

Method Source Code

/*/* w w  w .  j a  v  a2  s. c  o m*/
   Format date to string
   @format {String}
   return {String}
*/
Date.prototype.format = function(format) {
   var self = this;

   var h = self.getHours();
   var m = self.getMinutes().toString();
   var s = self.getSeconds().toString();
   var M = (self.getMonth() + 1).toString();
   var yyyy = self.getFullYear().toString();
   var yy = self.getYear().toString();
   var d = self.getDate().toString();

   var a = 'AM';
   var H = h.toString();

   if (h >= 12) {
      h -= 12;
      a = 'PM';
   }

   if (h === 0)
      h = 12;

   h = h.toString();

   var hh = h.padLeft(2, '0');
   var HH = H.padLeft(2, '0');
   var mm = m.padLeft(2, '0');
   var ss = s.padLeft(2, '0');
   var MM = M.padLeft(2, '0');
   var dd = d.padLeft(2, '0');

   if (format === undefined || format === null || format === '')
      return yyyy + '-' + MM + '-' + dd + 'T' + hh + ':' + mm + ':' + ss + ':' + self.getMilliseconds().toString();

   return format.replace(/yyyy/g, yyyy).replace(/yy/g, yy).replace(/MM/g, MM).replace(/M/g, M).replace(/dd/g, dd).replace(/d/g, d).replace(/HH/g, HH).replace(/H/g, H).replace(/hh/g, hh).replace(/h/g, h).replace(/mm/g, mm).replace(/m/g, m).replace(/ss/g, ss).replace(/s/g, ss).replace(/a/g, a);
};

Related

  1. format(format)
    Date.prototype.format = function(format) {
      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(format)
    Date.prototype.format = function(format) {
      if (format === undefined)
        return this.toString();
      var formatSeparator = format.match(/[.\/\-\s].*?/);
      var formatParts     = format.split(/\W+/);
      var result          = '';
      for (var i=0; i<=formatParts.length; i++) {
        switch(formatParts[i]) {
          case 'd':
    ...
    
  3. format(format)
    Date.prototype.format = function(format) {
        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()
    ...
    
  4. format(format)
    Date.prototype.format = function (format) {
        format = format || "yyyy-MM-dd";
        var args = {
            "M+": this.getMonth() + 1,
            "d+": this.getDate(),
            "h+": this.getHours(),
            "m+": this.getMinutes(),
            "s+": this.getSeconds(),
            "q+": Math.floor((this.getMonth() + 3) / 3),  
    ...
    
  5. format(format)
    Date.prototype.format = function(format) 
      var monName = [ "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" ];
      var dayName = [ "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat" ];
      var o = {
        "M+" : this.getMonth() + 1, 
        "d+" : this.getDate(), 
        "h+" : this.getHours(), 
        "i+" : this.getHours() + 1, 
    ...
    
  6. format(format)
    var fs = require('fs');
    var readline = require('readline');
    Date.prototype.format = function(format) {
      var o = {
        "M+" : this.getMonth()+1, 
        "d+" : this.getDate(),    
        "h+" : this.getHours(),   
        "m+" : this.getMinutes(), 
        "s+" : this.getSeconds(), 
    ...
    
  7. 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(),
    ...
    
  8. 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(".");
    ...
    
  9. 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(".");
    ...