JS格式化時間的幾種方法總結(jié)
更新時間:2022年04月26日 11:22:29 投稿:yangbin
這篇文章介紹了JS格式化時間的方法,文中通過示例代碼介紹的非常詳細(xì)。對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
方法一
方法
//格式化時間 function format(dat){ //獲取年月日,時間 var year = dat.getFullYear(); var mon = (dat.getMonth()+1) < 10 ? "0"+(dat.getMonth()+1) : dat.getMonth()+1; var data = dat.getDate() < 10 ? "0"+(dat.getDate()) : dat.getDate(); var hour = dat.getHours() < 10 ? "0"+(dat.getHours()) : dat.getHours(); var min = dat.getMinutes() < 10 ? "0"+(dat.getMinutes()) : dat.getMinutes(); var seon = dat.getSeconds() < 10 ? "0"+(dat.getSeconds()) : dat.getSeconds(); var newDate = year +"-"+ mon +"-"+ data +" "+ hour +":"+ min +":"+ seon; return newDate; }
調(diào)用
//獲取時間 var dat = new Date(); //格式化時間 var newDate = format(dat);
方法二
/** * 格式化日期 * @param {string | number | Date} value 指定日期 * @param {string} format 格式化的規(guī)則 * @example * ```js * formatDate(); * formatDate(1603264465956); * formatDate(1603264465956, "h:m:s"); * formatDate(1603264465956, "Y年M月D日"); * ``` */ function formatDate(value = Date.now(), format = "Y-M-D h:m:s") { const formatNumber = n => `0${n}`.slice(-2); const date = new Date(value); const formatList = ["Y", "M", "D", "h", "m", "s"]; const resultList = []; resultList.push(date.getFullYear().toString()); resultList.push(formatNumber(date.getMonth() + 1)); resultList.push(formatNumber(date.getDate())); resultList.push(formatNumber(date.getHours())); resultList.push(formatNumber(date.getMinutes())); resultList.push(formatNumber(date.getSeconds())); for (let i = 0; i < resultList.length; i++) { format = format.replace(formatList[i], resultList[i]); } return format; }
方法三
獲得當(dāng)前時間,并格式化
function getNowFormatDate() { var date = new Date(); var year = date.getFullYear(); var month = date.getMonth() + 1; var d = date.getDate(); var hour = date.getHours(); var minute = date.getMinutes(); var second = date.getSeconds(); if(month<10){ month = "0" + month; } if(d<10){ d = "0" + d; } if(hour<10){ hour = "0" + hour; } if(minute<10){ minute = "0" + hour; } if(second<10){ second = "0" + second; } return year + "-" + month + "-" + d + " " +hour + ":" + minute + ":" + second; }
注意這里的月要加1。
方法四
方法
// 對Date的擴(kuò)展,將 Date 轉(zhuǎn)化為指定格式的String // 月(M)、日(d)、小時(h)、分(m)、秒(s)、季度(q) 可以用 1-2 個占位符, // 年(y)可以用 1-4 個占位符,毫秒(S)只能用 1 個占位符(是 1-3 位的數(shù)字) // 例子: // (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 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), //季度 "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; }
調(diào)用
$(document).ready(function(){ //給時間控件加上樣式 $("#tb").find("input[name='cruMon_begin']").attr("class","Wdate").click(function(){WdatePicker({dateFmt:'yyyy-MM'});}); $("#tb").find("input[name='cruMon_end']").attr("class","Wdate").click(function(){WdatePicker({dateFmt:'yyyy-MM'});}); $("#tb").find("input[name='cruMon_begin']").attr("value",new Date().Format("yyyy-MM")); $("#tb").find("input[name='cruMon_end']").attr("value",new Date().Format("yyyy-MM")); });
方法五
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 time = new Date().format("yyyy-MM-dd hh:mm:ss");
到此這篇關(guān)于JS格式化時間的文章就介紹到這了。希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
js+html5實(shí)現(xiàn)側(cè)滑頁面效果
這篇文章主要為大家詳細(xì)介紹了js+html5實(shí)現(xiàn)側(cè)滑頁面效果,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-07-07使用postMesssage()實(shí)現(xiàn)iframe跨域頁面間的信息傳遞
這篇文章主要介紹了使用postMesssage()實(shí)現(xiàn)iframe跨域頁面間的信息傳遞 的相關(guān)資料,需要的朋友可以參考下2016-03-03