Time to timestamp

Parse (new Date()) 2, Number(new Date()) 3, +new Date() 4, var Date = new Date() console.log(date.gettime ())Copy the code

Time stamp to time (year month day, hour minute second)

1. Method 1

function(time) { var date = new Date(time); Let Y = date.getFullYear() + '-'; let Y = date.getFullYear() + '-'; let M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1) + '-'; let D = (date.getDate() + '').padStart(2, '0') + ' '; let h = (date.getHours() + '').padStart(2, '0') + ':'; let m = (date.getMinutes() + '').padStart(2, '0') + ':'; let s = (date.getSeconds() + '').padStart(2, '0'); return Y + M + D + h + m + s; }Copy the code

2. Method two

NPM install moment -s install moment -s import moment from 'moment' 2.3, use moment(timestamp). Format ("YYYY-MM-DD HH: MM :ss"); // If the timestamp is 10 digits, the timestamp is multiplied by 1000. If the timestamp is 13 digits, the timestamp is not multiplied by 1000Copy the code