timedddd
Version:
测试格式化时用的,千万别下
42 lines (40 loc) • 1.43 kB
JavaScript
// 格式化日期
function dateFormat(fmt, date) {
let ret;
const opt = {
"Y+": date.getFullYear().toString(), // 年
"m+": (date.getMonth() + 1).toString(), // 月
"d+": date.getDate().toString(), // 日
"H+": date.getHours().toString(), // 时
"M+": date.getMinutes().toString(), // 分
"S+": date.getSeconds().toString() // 秒
// 有其他格式化字符需求可以继续添加,必须转化成字符串
};
for (let k in opt) {
ret = new RegExp("(" + k + ")").exec(fmt);
if (ret) {
fmt = fmt.replace(ret[1], (ret[1].length == 1) ? (opt[k]) : (opt[k].padStart(ret[1].length, "0")))
};
};
return fmt;
}
// 时间戳的格式化时间
function timeFormat(time) {
var date = new Date()
var now = now.setTime(time);
var year=now.getFullYear();
var month=now.getMonth()+1;
var date=now.getDate();
var hour=now.getHours();
var minute=now.getMinutes();
var second=now.getSeconds();
var s = year + "/" + Appendzero (month) + "/" + Appendzero (date) + " " + Appendzero (hour) + ":" + Appendzero (minute) + ":" + Appendzero (second);
return s;
}
function Appendzero (obj) {
if (obj < 10) return "0" + obj; else return obj;
}
module.exports = {
timeFormat,
dateFormat
}