This article mainly introduces the related information of javascriptDateformat for JS date formatting, for more information about js operations on the Date object, see the previous article ), this article introduces javascript Date format for js Date formatting. Three methods are provided in this Article. For details, see the following.
Method 1:
// Extend the Date to String // month (M), Day (d), hour (h), and minute (m) in the specified format), second (s), quarter (q) can use 1-2 placeholders, // year (y) can use 1-4 placeholders, Millisecond (S) only one placeholder (a number ranging from 1 to 3) can be used. // example: // (new Date ()). format ("yyyy-MM-dd hh: mm: ss. S ") => 08:09:04. 423 // (new Date ()). format ("yyyy-M-d h: m: s. S ") ==>. 18 Date. prototype. format = function (fmt) {// author: meizz var o = {"M +": this. getMonth () + 1, // month "d +": this. getDate (),// Day "h +": this. getHours (), // Hour "m +": this. getMinutes (), // minute "s +": this. getSeconds (), // second "q +": Math. floor (this. getMonth () + 3)/3), // quarter "S": this. getMilliseconds () // millisecond}; if (/(y + )/. test (fmt) fmt = fmt. replace (RegExp. $1, (this. getFullYear () + ""). substr (4-RegExp. $1. length); for (var k in o) if (new RegExp ("(" + k + ")"). test (fmt) fmt = fmt. replace (RegExp. $1, (RegExp. $1. length = 1 )? (O [k]): ("00" + o [k]). substr ("" + o [k]). length); return fmt ;}
Call method:
var time1 = new Date().Format("yyyy-MM-dd");var time2 = new Date().Format("yyyy-MM-dd HH:mm:ss");
Method 2: