javaScript new Date()开课啦~

1 2 3 4 5 6 7 8 9 10 11 12 13
| date.getYear(); date.getFullYear(); date.getMonth(); date.getDate(); date.getDay(); date.getTime(); date.getHours(); date.getMinutes(); date.getSeconds(); date.getMilliseconds(); date.toLocaleDateString(); date.toLocaleTimeString(); date.toLocaleString( );
|
1 2 3 4 5 6 7 8
| date.setTime(value) date.setYear(val) date.setMonth(val) date.setDate(val) date.setDay(val) date.setHours(val) date.setMinutes(val) date.setSeconds(val)
|
1.时间戳转YYYY-HH-DD HH:mm:ss
代码如下:
1 2 3 4 5 6 7 8 9 10 11
| formatDate(time) { const date = new Date(time) const Y = date.getFullYear() const M = date.getMonth() + 1 const D = date.getDate() const h = date.getHours() const m = date.getMinutes() const s = date.getSeconds() return Y + '-' + (M < 10 ? '0' + M : M) + '-' + (D < 10 ? '0' + D : D) + ' ' + (h < 10 ? '0' + h : h) + ':' + (m < 10 ? '0' + m : m) + ':' + (s < 10 ? '0' + s : s) }
|
2.当前时间加上某某小时
可能存在时间戳、YYYY-HH-DD HH:mm:ss等这样的时间可以先进行时间转换~
1 2 3 4 5 6
| addTime(hour) { const date = new Date() date.setHours(date.getHours() + hour) return date },
|