沂源放心企业网站建设方案报价,线上平台推广方案,网页制作元素有哪些,品牌设计包括哪些要素javascript【格式化时间日期】 操作#xff1a;
(1) 日期格式化代码
/*** 日期格式化函数br/* 调用格式#xff1a;需要使用日期对象调用* p new Date().Format(yyyy/MM/dd HH:mm:ss); /p* param fmt 日期格式* returns {*} 返回格式化…javascript【格式化时间日期】 操作
(1) 日期格式化代码
/*** 日期格式化函数br/* 调用格式需要使用日期对象调用* p new Date().Format(yyyy/MM/dd HH:mm:ss); /p* param fmt 日期格式* returns {*} 返回格式化后的日期* constructor*/
Date.prototype.Format function (fmt) {var object {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 pattern in object) {if (new RegExp(( pattern )).test(fmt)) {fmt fmt.replace(RegExp.$1, (RegExp.$1.length 1) ? (object[pattern]) : ((00 object[pattern]).substr(( object[pattern]).length)));}}return fmt;
}(2) 函数调用
使用一个Date对象去调用Format函数即可。 时间戳转换如果是时间戳需要转换则需要注意的是时间戳是10位长度的还是13位长度的*如果是10位长度的时间戳那么就需要乘以1000后在调用Format进行转换*。 举例如下
// 调用函数
// 年月日
var date1 new Date().Format(yyyy/MM/dd)
console.log(date1)
// 年月日 时分秒 毫秒
var date2 new Date().Format(yyyy/MM/dd HH:mm:ss.S)
console.log(date2)
// 年月日 时分秒 毫秒 季度
var date3 new Date().Format(yyyy/MM/dd HH:mm:ss.S qq)
console.log(date3)
// 时间戳
var date4 new Date(1609430400000).Format(yyyy/MM/dd HH:mm:ss.S qq)
console.log(date4)代码运行结果如下