我正在参加「掘金·启航计划」
按照需求,要展示当前的时间,每秒更新一次。 只能先获取,在做下一步操作。
const getCurrentTime = () => {
let _time = new Date()
let y = String(_time.getFullYear())
let m = String(_time.getMonth() + 1 < 10 ? '0' + (_time.getMonth() + 1) : _time.getMonth() + 1)
let d = String(_time.getDate() < 10 ? '0' + _time.getDate() : _time.getDate())
let hh = String(_time.getHours() < 10 ? '0' + _time.getHours() : _time.getHours())
let mm = String(_time.getMinutes() < 10 ? '0' + _time.getMinutes() : _time.getMinutes())
let ss = String(_time.getSeconds() < 10 ? '0' + _time.getSeconds() : _time.getSeconds())
return { y, m, d, hh, mm, ss, }
}
返回的格式为:{ y: '2022', m: '06', d: '12', hh: '08', mm: '40', ss: '53' }
可以根据自己的实际情况来设计需要的格式,
主要思路是通过js Data类型的获取年、月、日、时、分、秒的方法来得到具体的年月日时分秒。然后根据自己的要求来搭配自己的格式。