When typing, typing, typing code, encounter date display, each time to format again, using Python time.strftime, feel really tm convenient, decided to wheel.

Time and date formatting symbols in Python

%y two-digit years represent (00-99)

%Y four-digit year represents (000-9999)

% M Month (01-12)

%d One day in a month (0-31)

%H Number of 24-hour hours (0-23)

%I Number of hours in 12-hour system (01-12)

%M Minutes (00=59)

%S seconds (00-59)

%a Local simplified week name

%A Local full week name

%b Local simplified month name

%B Local full month name

%c Local corresponding date and time representation

% J One day in a year (001-366)

%p Local A.M. or P.M. equivalent

%U Number of weeks of the year (00-53) Sunday is the beginning of the week

% W Week (0-6), Sunday is the beginning of the week

%W Number of weeks of the year (00-53) Monday is the beginning of the week

%x Local corresponding date representation

%X Local time representation

%Z Specifies the name of the current time zone

%% % number itself

Some matches will be used less than once in a lifetime, and some think of Weex’s Write it once, run multiple, I will also Write it once,use it for ever (Write it once,use it for ever)

Write it once,use it for ever

First, I’ll write a couple of widgets :(I put these common functions on Date)

Date format judgment

if (!Date.isDate) {// Is the date format
    Object.defineProperty(Date. 'isDate'. {
        value: function (date) {
		return Object.prototype.toString.call(date) = = = "[object Date]";
	},
        configurable: true.
        enumerable: false.
        writable: true
    });
}
Copy the code

Leap year judgment

if (!Date.isLeapYear) {
    Object.defineProperty(Date. 'isLeapYear'. {
        value: function (year) {// Is it a leap year
		year = year >>> 0;// Arguments must be positive integers
		return (year % 400 = = 0) || (year % 4 = = 0 && year % 100 ! = 0);
	},
        configurable: true.
        enumerable: false.
        writable: true
    });
}
Copy the code

Get the number of days in a month (function not wrapped on Date)

function getMonthDays(year. month) {// The number of days in a month, month (1-12), the best parameter type is integer, otherwise there will be unexpected results
	var date;// If no argument is passed, a today-based date is created by default
	if (Date.isDate(arguments[0])) {// If the date format is used as the argument
		date = arguments[0];
		year = date.getFullYear(a);
  		month = date.getMonth(a) + 1;
	} else {
		date = new Date(a);
		year = year > 0 ? year >>> 0 : date.getFullYear(a);
		month = month > 0 ? month % 13 : date.getMonth(a) + 1;
	}
	return [31. null. 31. 30. 31. 30. 31. 31. 30. 31. 30. 31] [month - 1] || (Date.isLeapYear(year) ? 29 : 28);
}
Copy the code

Get a date is the day of the year

function getDayNumber(year. month. day) {// The argument had better be an integer, otherwise there will be unexpected results
	var date;// If no argument is passed, a today-based date is created by default
	if (Date.isDate(arguments[0])) {// If the date format is used as the argument
		date = arguments[0];
		year = date.getFullYear(a);
  		month = date.getMonth(a) + 1;
	} else {
		date = new Date(a);
		year = year > 0 ? year >>> 0 : date.getFullYear(a);
		month = month > 0 ? month % 13 : date.getMonth(a) + 1;
		day = day > 0 ? day >>> 0 : date.getDate(a);
	}
	// Day of the year (001-366)
	for (var i = 1; i < month; i++) {
	    day + = getMonthDays(year. i);
	}
	return day;
}
Copy the code

Pig’s foot debut: Date and time formatting

if (!Date.prototype.format) {
	Object.defineProperty(Date.prototype. "format". {
        value: Date.prototype.format = function (fmt) {
		var week = ['Sunday'. 'Monday'. 'Tuesday'. 'Wednesday'. 'Thursday'. 'Friday'. 'Saturday'];
		var weekShortName = ['Sun'. 'Mon'. 'Tues'. 'Wed'. 'Thurs'. 'Fri'. 'Sat'];
		var month = ["December". "January". "February". "March". "April". "May". "June". "July". "August". "September". "October". "November"]
		var monthShortName = ["Dec". "Jan". "Feb". "Mar". "Apr". "May". "Jun". "Jul". "Aug". "Sep". "Oct". "Nov"];
		var regs = {
			'%y': this.getFullYear(a) % 100.// Two digit years represent (00-99)
			'%Y': this.getFullYear(),// 4-digit year representation (000-9999)
			'%m': this.getMonth(a) + 1.// Month (01-12)
			'%d': this.getDate(),// One day in a month (0-31)
			'%H': this.getHours(),// The number of hours in 24 hours (0-23)
			'%I': this.getHours(a) % 12 + 1.// The number of hours in 12-hour system (01-12)
			'%M': this.getMinutes(),// Minutes (00=59)
			'%S': this.getSeconds(),// second (00-59)
			'%a': weekShortName[this.getDay()].// Local simplified week name
			'%A': week[this.getDay()].// Local full week name
			'%b': monthShortName[this.getMonth()].// Local simplified month names
			'%B': month[this.getMonth()].// Local full month name
			'%c': this.toLocaleString(),// Local date representation and time representation
			'%j': getDayNumber(),// Day of the year (001-366)
			'%u': Math.ceil(getDayNumber(a) / 7),// Number of weeks of the year (00-53) Monday is the beginning of the week
			'%U': this.getDay(a) = = 0 ? parseInt(getDayNumber(a) / 7) + 1 : parseInt(getDayNumber(a) / 7),// Number of days of the year (00-53) Sunday is the beginning of the week
			'%w': this.getDay(),// Sunday is the beginning of the week
			"%q": Math.ceil((this.getMonth(a) + 1) / 3), / / quarter
			'%x': this.toLocaleDateString(),// Local corresponding date representation
			'%X': this.toLocaleTimeString(),// Local corresponding time representation
		};
		// Format the match
		for (var k in regs) {
			if (new RegExp("(" + k + ")").test(fmt)) {
				fmt = fmt.replace(RegExp.The $1. regs[k]);
			}
                }
		return fmt;
	},
        configurable: true.
        enumerable: false.
        writable: true
	});
}
Copy the code

Take a chestnut

var d = new Date(a);
console.log(d.format("%y--%Y--%m--%d--%H--%I--%M--%S--%a--%A--%b--%B--%c--%j--%u--%U--%w--%q--%x--%X"))
Copy the code

The results show

Here’s where the source code is

JavaScript date and time formatting

If there are mistakes, welcome to correct!