100字范文,内容丰富有趣,生活中的好帮手!
100字范文 > JS 获取当前日期时间/时间日期格式化(时间戳 转 yyyy-MM-dd HH:mm:ss)

JS 获取当前日期时间/时间日期格式化(时间戳 转 yyyy-MM-dd HH:mm:ss)

时间:2022-08-15 20:12:55

相关推荐

JS 获取当前日期时间/时间日期格式化(时间戳 转 yyyy-MM-dd HH:mm:ss)

// 不输入参数调用的就是当前时间// 参数--需转换时间的时间戳function formatDate(time = new Date()) {let date = new Date(time);let YY = date.getFullYear();let MM = date.getMonth() + 1 < 10 ? "0" + (date.getMonth() + 1) : date.getMonth() + 1;let DD = date.getDate() < 10 ? "0" + date.getDate() : date.getDate();let hh = date.getHours() < 10 ? "0" + date.getHours() : date.getHours();let mm = date.getMinutes() < 10 ? "0" + date.getMinutes() : date.getMinutes();let ss = date.getSeconds() < 10 ? "0" + date.getSeconds() : date.getSeconds();// 这里修改返回时间的格式return YY + "-" + MM + "-" + DD + " " + hh + ":" + mm + ":" + ss;}

推荐使用:moment:/ 针对时间方面的功能很齐全,而且支持多端适配(例如IOS)

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。