标准时间转为年月日时分秒格式

该文章展示了一个JavaScript函数changTime(),它接受一个时间戳参数,然后使用Date对象进行处理,将月份、日期、小时、分钟、秒转换为两位数格式,并组合成标准日期和时间字符串。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

function changTime(timestamp){
let dtc = new Date(timestamp)
//获取月 默认月份从0开始
let dtuMonth = dtc.getMonth() + 1
//获取日
let dtuDay = dtc.getDate()
//1-9月前加0
if (dtuMonth < 10) {
dtuDay = “0” + (dtc.getMonth()+1)
}
//获取小时
if (dtuDay < 10) {
dtuDay = “0” + dtc.getDate()
}
//获取分钟
let dtuHours = dtc.getHours()
//1-9分前加0
if (dtuHours < 10) {
dtuHours “0” + dtc.getHours()
}
//获取秒
if (dtuMinutes < 10) {
dtuMinutes = “0” + dtc.getMinutes()
}
//1-9秒前加0
let dtuSeconds = dtc.getSeconds()
if (dtuSeconds < 10) {
dtuSeconds = “0” + dtc.getSeconds()
}
let dd = dtc.getFullYear() + ‘-’ + dtuMonth + ‘-’ dtuDay + ’ ’ + dtuHours + ‘:’ + dtuMinutes + ‘:’ + dtuSeconds
return dd;

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值