月(M)、日(d)、小时(h)、分(m)、秒(s)、季度(q) 可以用 1-2 个占位符, 年(y)可以用 1-4 个占位符,毫秒(S)只能用 1 个占位符(是 1-3 位的数字) (new Date()).Format("yyyy-MM-dd HH:mm:ss.S") ==> 2006-07-02 08:09:04.423 // (new Date()).Format("yyyy-MM-dd HH:mm:ss") ==> 2006-07-02 08:09:04
//
(new Date()).Format("yyyy-M-d H:m:s.S")      ==> 2006-7-2 8:9:4.18
Date.prototype.Format = function (fmt) { author: meizz 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), this .getMilliseconds() 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; var time1 = new Date().Format("yyyy-MM-dd"); var time2 = new Date().Format("yyyy-MM-dd HH:mm:ss"); * * 对Date的扩展,将 Date 转化为指定格式的String * 月(M)、日(d)、12小时(h)、24小时(H)、分(m)、秒(s)、周(E)、季度(q)
可以用 1-2 个占位符 * 年(y)可以用 1-4 个占位符,毫秒(S)只能用 1 个占位符(是 1-3 位的数字) * eg: * (new
Date()).pattern("yyyy-MM-dd hh:mm:ss.S")==> 2006-07-02 08:09:04.423
* (new Date()).pattern("yyyy-MM-dd E HH:mm:ss") ==> 2009-03-10 二 20:09:04
* (new Date()).pattern("yyyy-MM-dd EE hh:mm:ss") ==> 2009-03-10 周二 08:09:04
* (new Date()).pattern("yyyy-MM-dd EEE hh:mm:ss") ==> 2009-03-10 星期二 08:09:04
* (new Date()).pattern("yyyy-M-d h:m:s.S") ==> 2006-7-2 8:9:4.18

Date.prototype.pattern= function (fmt) { var o = {
"M+" : this .getMonth()+1,
"d+" : this .getDate(),
"h+" : this .getHours()%12 == 0 ? 12 : this .getHours()%12,
"H+" : this .getHours(),
"m+" : this .getMinutes(),
"s+" : this .getSeconds(),
"q+" : Math.floor(( this .getMonth()+3)/3), //季度
"S" : this .getMilliseconds() var week = {
"0" : "/u65e5",
"1" : "/u4e00",
"2" : "/u4e8c",
"3" : "/u4e09",
"4" : "/u56db",
"5" : "/u4e94",
"6" : "/u516d" if (/(y+)/.test(fmt)){
fmt=fmt.replace(RegExp.$1, ( this .getFullYear()+"").substr(4 - RegExp.$1.length)); if (/(E+)/.test(fmt)){
fmt=fmt.replace(RegExp.$1, ((RegExp.$1.length>1) ? (RegExp.$1.length>2 ? "/u661f/u671f" : "/u5468") : "")+week[ this .getDay()+""]); for ( var k in o){ 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; var date = new Date();
window.alert(date.pattern("yyyy-MM-dd hh:mm:ss"));
</script> case 'dddd': return ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'][d.getDay()]; case 'M': return d.getMonth() + 1; case 'MM': return zeroize(d.getMonth() + 1); case 'MMM': return ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'][d.getMonth()]; case 'MMMM': return ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'][d.getMonth()]; case 'yy': return String(d.getFullYear()).substr(2); case 'yyyy': return d.getFullYear(); case 'h': return d.getHours() % 12 || 12; case 'hh': return zeroize(d.getHours() % 12 || 12); case 'H': return d.getHours(); case 'HH': return zeroize(d.getHours()); case 'm': return d.getMinutes(); case 'mm': return zeroize(d.getMinutes()); case 's': return d.getSeconds(); case 'ss': return zeroize(d.getSeconds()); case 'l': return zeroize(d.getMilliseconds(), 3); case 'L': var m = d.getMilliseconds(); if (m > 99) m = Math.round(m / 10); return zeroize(m); case 'tt': return d.getHours() < 12 ? 'am' : 'pm'; case 'TT': return d.getHours() < 12 ? 'AM' : 'PM'; case 'Z': return d.toUTCString().match(/[A-Z]+$/); Return quoted strings with the surrounding quotes removed default : return $0.substr(1, $0.length - 2); 方法一:// 对Date的扩展,将 Date 转化为指定格式的String// 月(M)、日(d)、小时(h)、分(m)、秒(s)、季度(q) 可以用 1-2 个占位符, // 年(y)可以用 1-4 个占位符,毫秒(S)只能用 1 个占位符(是 1-3 位的数字) // 例子: // (new Date()).Format("yyyy-MM-dd hh:mm:ss.S
在上篇文章给大家介绍了 js Date 对象的操作的问题(生成一个倒数7天的数组),本篇介绍有关 js 日期格式化 javascript Date format ,本文通过三种方法给大家讲解,具体内容请看下文。 // 对 Date 的扩展,将 Date 转化为指定格式的String // 月(M)、日(d)、小时(h)、分(m)、秒(s)、季度(q) 可以用 1-2 个占位符, // 年(y)可以用 1-4 个占位符,毫秒(S)只能用 1 个占位符(是 1-3 位的数字) // 例子: // (new Date ()). Format ("yyyy-MM-dd hh:mm:ss.S") ==> 200
格式化 日期 函数,前辈们已经封装了很多已经有的函数,在需要用的时候直接拿过来用就好了。这里主要对用过的 format Date 进行记录以及加上自己的理解。代码以及注释片段如下: * 参数: * 1. date : 秒级时间; * 2. fmt: 格式化 的格式 'yyyy', 'yyyy-MM-dd', 'yy-MM-dd', 'yyyy-MM-dd hh:mm:ss', ...(按需求自己定义格式) * fmt给一个默认参
Js 时,有个地方用到 日期 ,要求是yyyy--MM--dd的格式,于是想到了 format 函数 //当前时间var Time = new Date (). format ("yyyy-MM-dd");但是当时 format 是报红的,后来才知道,需要在页面加载 js 时调用一下时间函数,代码如下: $(function () { datatime(); }); //时间函数functi...
第一种: Date .prototype. format = function( format ) { var o ={ "M+" : this.getMonth()+1, //month "d+" : this.get Date (), //day "h+" : this.getHours(), //hour "m+" : this.ge
Date .prototype. format = function(mask) {     var d = this;     var zeroize = function(value, length) {         if (!length) length = 2;
// 对 Date 的扩展,将 Date 转化为指定格式的String // 月(M)、日(d)、小时(h)、分(m)、秒(s)、季度(q) 可以用 1-2 个占位符, // 年(y)可以用 1-4 个占位符,毫秒(S)只能用 1 个占位符(是 1-3 位的数字) // 例子: // (new Date ()). Format ("yyyy-MM-dd hh:mm:ss.S") ==> 2006-07-02 08:09:04.423 // (new Date ()). Format ("yyyy-M-d h:m:s.S") ==> 2006-7-2 8:9:4.18 // (new Date (2006/7/2 8:9:4.18)). Format ("yyyy-M-d hh:mm:ss.S") ==> 2006-07-02 8:9:4.18
function format ( date ) { let format = “YYYY-MM-DD hh:mm:ss” let year = date .getFullYear(); let month = date .getMonth() let day = date .getDay() let hours = date .getHours() let minutes = date .getMinutes() let seconds = date .getSeconds() let config = { 也可以使用其他方法如 `.to Date String()` , `.toTimeString()`, `.toUTCString()`等来 格式化 日期 输出。 或者使用moment. js 这个第三方库来帮助你 格式化 日期 var moment = require('moment'); console.log(moment(). format ('YYYY-MM-DD HH:mm:ss'));