年月日时分秒格式的转换为时间戳。

一般我们经常遇见的就是 时间戳转换为时分秒的, 但是这次做的是一个日期组件,后台要求要传时间戳格式的,还是比较少见的。
下面就告诉大家怎么做
2020-03-16 12:00:00 原始数据
转成:1584417600 这种是秒格式的,还有hs格式的把1000/去掉就行了

1:新建文件,写成封装,方便使用

const publicMethod = {

getTimestamp(time) { //把时间日期转成时间戳
return (new Date(time)).getTime() / 1000
}

}
export {
publicMethod
}

2:在所需要的文件引入封装的js

import { publicMethod } from “@/utils”;
//使用
getEditorSave() {
publicMethod.getTimestamp(‘2019-08-27 00:03:04’)
}
\

获取当前时间的
var pre = new Date()
pre.setFullYear(pre.getFullYear() - 1)
console.log(pre)// Date 2016-11-07T03:19:47.052Z
var now = new Date()
console.log(now)// Date 2017-11-07T03:19:47.054Z

你可能感兴趣的:(年月日时分秒格式的转换为时间戳。)