JS简单实现的日期格式转换
将日期传入函数可返回格式化好的日期格式,第二个参数可自定义年月日之间的连接符号默认为短横线'-'
// 格式化时间格式
function formatDate(date, link = '-') {
var newDate = new Date(date) || new Date()
var year = newDate.getFullYear()
var month = newDate.getMonth() + 1
var day = newDate.getDate()
return year + link + (month < 10 ? '0' + month : month) + link + (day < 10 ? '0' + day : day)
},
var date = "2021.6.17"
formatDate(date) // 返回的时间格式 2021-6-17
formatDate(date,'/') // 返回的时间格式 2021/6/17