JavaScript: 如何正确格式化日期?
版权声明:转载请附上文章地址DJyzh_java基础,数据结构,java高级-****博客
首先简单介绍一下日期对象:
【1】日期对象
在javascript中并没有日期型的数据类型,提供了一个日期对象可以操作日期和时间。
【2】日期对象转换为字符串
date.toString0://将日期对象转换为字符串时,采用的是本地时间
date.toLocalString0;//将日期对象转换为字符串,采用的是本地时间,显示的是地方日期的格式
date.toUTCString0://将日期对象转换为字符串时,采用的是世界时间。
【3】日期对象中的日期和时间转换为字符串
date.toDateString0://将日期部分转换为字符串,本地时间
date.toLocalDateString0://将日期部分转换为字符串,采用的是本地时间,显示的是地方日期的格式date.toTimeString0://将时间部分转换为字符串,本地时间
date.getFullYear0://获取年份,, 以四位数显式,建议使用
date.getMonth0://获取月份,值为0-11 ,一月份为0 ,二月份为1... date.getDate0;//获取天数,即一个月中的某一天
date.getDayo://获取-周中的第几天,值为0-6 ,周日为0... :
【4】日期对象中的时间
date.getHours0://返回小时部分date.getMinutes(://返回分钟部分date.getSeconds0://只返回日期对象时刻的秒部分
date.getMilliseconds0;//只返回日期对象时刻的毫秒部分
date.getTime0;//返回日期对象中的时间与1970年1月1日0时0分0秒所间隔的毫秒数date.getTimezoneoffset0://返回日期对象中的时间与UTC之间的时差数,单位为秒。
javascript Date format(js日期格式化)
方法1
/**
/*对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") ==> 2019-01-02 10:19:04.423
/* (new Date()).Format("yyyy-M-d h:m:s.S") ==> 2019-1-2 10:19:4.18
*/
Date.prototype.Format = function (fmt) {
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), //季度
"S": 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");
方法2
<script language="javascript" type="text/javascript">
<!-- /** * 对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")==> 2019-01-10 19:29:09.423
* (new Date()).pattern("yyyy-MM-dd E HH:mm:ss") ==> 2019-01-10 四 19:23:04
* (new Date()).pattern("yyyy-MM-dd EE hh:mm:ss") ==> 2019-01-10 周四 19:19:04
* (new Date()).pattern("yyyy-MM-dd EEE hh:mm:ss") ==> 2019-01-10 星期四19:06:04
* (new Date()).pattern("yyyy-M-d h:m:s.S") ==> 2019-01-10 19:09:04
*/
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){
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 date = new Date();
window.alert(date.pattern("yyyy-MM-dd hh:mm:ss"));
// -->
</script>
上一篇: JavaScript 时间戳格式化技巧
下一篇: 搞定时间格式与补全零头的技巧