Date.prototype.format = function(format){
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(format)) {
format = format.replace(RegExp.$1, (this.getFullYear()+"").substr(4 - RegExp.$1.length));
}
for(var k in o) {
if(new RegExp("("+ k +")").test(format)) {
format = format.replace(RegExp.$1, RegExp.$1.length==1 ? o[k] : ("00"+ o[k]).substr((""+ o[k]).length));
}
}
return format;
}
//使用方法
var now = new Date();
var nowStr = now.format("yyyy-MM-dd hh:mm:ss");
//使用方法2:
var testDate = new Date();
var testStr = testDate.format("YYYY年MM月dd日hh小时mm分ss秒");
alert(testStr);
//示例:
alert(new Date().Format("yyyy年MM月dd日"));
alert(new Date().Format("MM/dd/yyyy"));
alert(new Date().Format("yyyyMMdd"));
alert(new Date().Format("yyyy-MM-dd hh:mm:ss"));
分享到:
相关推荐
js日期时间格式化js日期时间格式化js日期时间格式化js日期时间格式化js日期时间格式化js日期时间格式化js日期时间格式化js日期时间格式化
js通用日期格式化工具,直接引用该工具类就可以使用,方便快捷,建议大家收藏此工具类,该工具很好的解决了前端页面显示日期格式问题
javascript格式化日期,将日期型转为字符串,字符串转为日期型,格式自由。
// 对Date的扩展,将 Date 转化为指定格式的String // 月(M)、日(d)、小时(h)、分(m)、秒(s)、季度(q) 可以用 1-2 个占位符, // 年(y)可以用 1-4 个占位符,毫秒(S)只能用 1 个占位符(是 1-3 位的数字) //...
JS日期格式化
JS 日期格式化和加减,适用于将日期格式转化为yyyy-MM-DD HH:mm:ss格式,以及日期的加减
javascript中Date format(js日期格式化)方法小结.docx
JQuery 的一个日期格式化插件,支持"MM-dd HH:mm"等格式的设定;并支持日期时间转义,即距当前过去了多久,如"3分钟前","1天前","2周前".
主要为大家详细介绍了Js获取当前日期时间及格式化代码,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
js日期格式化,对Date的扩展,将 Date 转化为指定格式的String。月(M)、日(d)、小时(h)、分(m)、秒(s)、季度(q) 可以用 1-2 个占位符, 年(y)可以用 1-4 个占位符,毫秒(S)只能用 1 个占位符(是 1-3 位的数字)
JS时间格式化,JS两个时间差,JS当前日期的昨天明天 ,JS当前日期周几
JavaScript - 格式化日期时间 思路代码:demo.html 完整代码:index.html 相关教程,请前往我的博客查看。
js 语言的格式化日期格式的类。很好用的。
javascript的TRIM函数和日期时间格式化函数 如 var d = new Date().format('yyyy-MM-dd hh:mm:ss');
兼容ios的日期格式化,兼容ios的日期格式化,兼容ios的日期格式化
moment().format('YYYY-MM-DD HH:mm:ss') 获取当前时间 并格式化
格式化后端序列化的对象中含有日期格式的Json数据。
JavaScript时间格式化插件,返回指定字符串