前端时间格式转换
时间: 2023-07-20 22:14:45 浏览: 51
你好,关于时间格式转换,前端常用的方法有以下几种:
1. 使用 Date 对象的方法来进行格式化,例如:
```
const date = new Date()
const year = date.getFullYear()
const month = date.getMonth() + 1
const day = date.getDate()
const hour = date.getHours()
const minute = date.getMinutes()
const second = date.getSeconds()
const formattedDate = `${year}-${month}-${day} ${hour}:${minute}:${second}`
console.log(formattedDate) // 输出格式化后的时间
```
2. 使用第三方库 moment.js 来进行格式化,例如:
```
const moment = require('moment')
const date = moment().format('YYYY-MM-DD HH:mm:ss')
console.log(date) // 输出格式化后的时间
```
3. 使用 ES6 中新增的 Intl 对象来进行格式化,例如:
```
const date = new Date()
const formattedDate = new Intl.DateTimeFormat('zh-CN', {
year: 'numeric',
month: '2-digit',
day: '2-digit',
hour: '2-digit',
minute: '2-digit',
second: '2-digit'
}).format(date)
console.log(formattedDate) // 输出格式化后的时间
```
以上是前端常用的时间格式转换方法,希望能对你有所帮助。