JS日期格式

 2023-09-09 阅读 19 评论 0

摘要://时间戳转换为年月日时分秒时间格式。 function dateFormat(date) {var date = new Date(date);Y = date.getFullYear() + '-';M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1) &

//时间戳转换为年月日时分秒时间格式。
function dateFormat(date) {var date = new Date(date);Y = date.getFullYear() + '-';M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1) + '-';D = (date.getDate() < 10 ? '0' + (date.getDate()) : date.getDate()) + ' ';h = (date.getHours() < 10 ? '0' + (date.getHours()) : date.getHours()) + ':';m = (date.getMinutes() < 10 ? '0' + (date.getMinutes()) : date.getMinutes()) + ':';s = (date.getSeconds() < 10 ? '0' + (date.getSeconds()) : date.getSeconds());return(Y + M + D + h + m + s );
}
alert(dateFormat(new Date());//时间戳转换为年月日时间格式。
function dateFormatShort(date) {var date = new Date(date);Y = date.getFullYear() + '-';M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1) + '-';D = (date.getDate() < 10 ? '0' + (date.getDate()) : date.getDate());return(Y + M + D);
}
alert(dateFormatShort(new Date());//时间戳转换为年月时间格式。
function dateFormatShort(date) {var date = new Date(date);Y = date.getFullYear() + '-';M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1);	return(Y + M);
}
alert(dateFormatShort(new Date());//年月日转时间戳
function dateFormattotime(date){var str = date;str = str.replace(/-/g,'/');var date = new Date(str);return date.getTime();
}
alert(dateFormattotime("2019-02-18")); //1550419200000

 

版权声明:本站所有资料均为网友推荐收集整理而来,仅供学习和研究交流使用。

原文链接:https://hbdhgg.com/1/31300.html

发表评论:

本站为非赢利网站,部分文章来源或改编自互联网及其他公众平台,主要目的在于分享信息,版权归原作者所有,内容仅供读者参考,如有侵权请联系我们删除!

Copyright © 2022 匯編語言學習筆記 Inc. 保留所有权利。

底部版权信息