时间格式转换

export function formatDate(date: any, format: string) {
format = format || “YYYY-MM-DD hh:mm:ss”; //第一个参数不填时,使用默认格式
let ret, renum;
const opt: any = {
“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 (var k in opt) {
ret = new RegExp(“(” + k + “+)”).exec(format);
if (ret) {
renum = (ret[1].length == 1) ? (opt[k]) : (opt[k].padStart(ret[1].length, “0”)) //根据复数前面是否补零,如“mm”补零,单“m”前面不补零
format = format.replace(ret[1], renum) //替换
};
};
return format;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值