1. Convert timestamp to 'YYYY-MM-DD HH:mm:ss' format - old way
Splicing is performed sequentially through the corresponding year, month, day, hour, minute, and second. In addition, values less than 10 need to be processed, and the string '0' is added in front to convert it into a common two-digit time format.
function transformTime(timestamp = +new Date()) {
if (timestamp) {
var time = new Date(timestamp);
var y = time.getFullYear();
var M = time.getMonth() + 1;
var d = time.getDate();
var h = time.getHours();
var m = time.getMinutes();
var s = time.getSeconds();
return y + '-' + addZero(M) + '-' + addZero(d) + ' ' + addZero(h) + ':' + addZero(m) + ':' + addZero(s);
} else {
return '';
}
}
function addZero(m) {
return m < 10 ? '0' + m : m;
}
transformTime(); // "2023-01-25 15:25:28"
The old method optimization idea:
new Date().toJSON gets the JSON string of Greenwich Mean Time, for example: '2023-01-25T15:39:11.803Z'
Converting to Beijing time requires an additional eight time zones, that is, adding eight hours to the current timestamp
What we need is the first 19 digits of the string, and then replace 'T' with a space, which is the time format we need
function time(time = +new Date()) {
var date = new Date(time + 8 * 3600 * 1000) // add 8 hours
return date
.toJSON()
.substr(0, 19)
.replace('T', ' ')
}
time() // '2023-01-25 15:39:11'
2. Time conversion plug-in in vue: moment.js
(1) Download the installation package
npm install moment --save
(2) Introduced in main.js
import moment from 'moment'
Vue.prototype.$moment = moment
(3) use
moment('time to convert').format('target conversion format');
this.$moment('Time to convert').format('YYYY-MM-DD')
For more time conversion formats, see the official website for details: Moment.js Chinese website
3. Vue dynamically displays real-time time/time formatting plugin: dayjs
(1) Download the installation package
npm install dayjs --save
(2) Introduced in main.js and used globally
import dayjs from "dayjs"
Vue.prototype.dayjs = dayjs;
(3) specific use
this.dayjs().format('YYYY-MM-DD') // current date: year month day
this.dayjs().format("YYYY-MM-DD HH:mm:ss") // current date: year, month, day, hour, minute, second
/* Get the time of the past seven days*/
for (let i = 6; i >= 0; i--) {
let date = this.dayjs(this.dayjs()-24*60*60*1000*i).format("YYYY-MM-DD")
console.log(date)
}
dayjs realizes clock effect - real-time display time
<template>
<div id="app">
{ {this.datetime}}
</div>
</template>
<script>
export default {
name: "app",
data() {
return {
timer: '',
datetime: ''
}
},
mounted() {
/* Timed refresh every second */
this.timer = setInterval(() => {
this.datetime = this.dayjs().format("YYYY-MM-DD HH:mm:ss")
console.log(this.datetime)
}, 1000)
},
beforeDestroy() {
/* Destroy the timer before leaving the page */
if(this.timer){
clearInterval(this.timer);
}
}
};
</script>
4. Convert the Excel date and time to standard time after uploading
function formatExcelDate(numb, format = '/') {
const time = new Date((numb - 25567) * 24 * 3600000 - 5 * 60 * 1000 - 43 * 1000 - 24 * 3600000 - 8 * 3600000)
time.setYear(time.getFullYear())
const year = time.getFullYear() + ''
const month = time.getMonth() + 1 + ''
const date = time.getDate() + ''
if (format && format.length === 1) {
return year + format + month + format + date
}
return year + (month < 10 ? '0' + month : month) + (date < 10 ? '0' + date : date)
}