Nodejs Date Format format(strFormat)

Here you can find the source of format(strFormat)

Method Source Code

/*/*from w w w.jav a  2  s  . c  o m*/
   DateFormat.js by Bruce Sedlacek @FuzzyBS
*/

Date.prototype.format = function (strFormat) {
   'use strict';
   var d = new Date(this),
      mon = ['', 'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'],
      month = ['', 'January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'],
      day = ['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat'],
      weekday = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'],
      v = {
         's': String(d.getSeconds()),
         'ss': (d.getSeconds() < 10 ? '0' : '') + d.getSeconds(),
         'm': String(d.getMinutes()),
         'mm': (d.getMinutes() < 10 ? '0' : '') + d.getMinutes(),
         'h': String((((d.getHours() + 11) % 12) + 1)),
         'hh': ((((d.getHours() + 11) % 12) + 1) < 10 ? '0' : '') + (((d.getHours() + 11) % 12) + 1),
         'H': String(d.getHours()),
         'HH': (d.getHours() < 10 ? '0' : '') + d.getHours(),
         'a': (d.getHours() < 12 ? 'am' : 'pm'),
         'aa': (d.getHours() < 12 ? 'a.m.' : 'p.m.'),
         'A': (d.getHours() < 12 ? 'AM' : 'PM'),
         'AA': (d.getHours() < 12 ? 'A.M.' : 'P.M.'),
         'd': String(d.getDate()),
         'dd': (d.getDate() < 10 ? '0' : '') + d.getDate(),
         'ddd': day[d.getDay()],
         'dddd': weekday[d.getDay()],
         'M': String(d.getMonth()),
         'MM': (d.getMonth() < 10 ? '0' : '') + d.getMonth(),
         'MMM': mon[d.getMonth()],
         'MMMM': month[d.getMonth()],
         'yy': String(d.getYear()),
         'yyyy': String(d.getFullYear()),
         'z': (d.getTimezoneOffset() > 0 ? '+' : '') + (Math.int(d.getTimezoneOffset() / 60) < 10 ? '0' : '') + String(Math.int(d.getTimezoneOffset() / 60)) + ((d.getTimezoneOffset() % 60) < 10 ? '0' : '') + String(d.getTimezoneOffset() % 60)
      };
   return strFormat.replace(/(s+|m+|h+|H+|a+|A+|d+|M+|y+|z+)/g, function (key) {
      return v[key] || key;
   });
};

Related

  1. 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(),
    ...
    
  2. format(pattern)
    Date.prototype.format = function(pattern)
      if(!pattern){
        pattern = "yyyy-MM-dd";
      var o =
        "M+" : this.getMonth() + 1, 
        "d+" : this.getDate(), 
    ...
    
  3. 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()
    ...
    
  4. 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];
    ...
    
  5. format(str)
    Date.prototype.format = function (str) {
      const o = {
        'y+': this.getFullYear(),
        'M+': this.getMonth() + 1,
        'd+': this.getDate(),
        'h+': this.getHours(),
        'm+': this.getMinutes(),
        's+': this.getSeconds()
      Object.keys(o).forEach(element => {
        str = str.replace(new RegExp(element), substr => {
          return String.prototype.padStart.call(o[element], substr.length, '0')
        })
      })
      return str
    
  6. format1()
    Date.prototype.format1 = function() {
        return "" + this.getMonth() + this.getFullYear();
    function f(d) {
        for (var i=0; i<60; i++) {
            assertEq(d.format1(), "91987");
    f(new Date("10/10/1987 1:11:11"));
    ...
    
  7. formatAMPM(date)
    function formatAMPM(date) {
      var hours = date.getHours();
      var minutes = date.getMinutes();
      var ampm = hours >= 12 ? 'pm' : 'am';
      hours = hours % 12;
      hours = hours ? hours : 12; 
      minutes = minutes < 10 ? '0'+minutes : minutes;
      var strTime = hours + ':' + minutes + ' ' + ampm;
      return strTime;
    ...
    
  8. formatDate(date)
    function formatDate(date) {
      if(date instanceof Date === false) {
        return console.log("err");
      var year = date.getFullYear();
      var month = pad(date.getMonth() + 1);
      var day = pad(date.getDate());
      return [year, month, day].join('-');
    
  9. formatDate(date)
    function formatDate(date){
      return date.split('-').reverse().join('/');