时间:var time = new Date();
// Tue Aug 28 2018 09:16:06 GMT+0800 (中国标准时间)
时间戳:var timestamp = Date.parse(time);
// 1535419062000 (Date.parse() 默认不取毫秒,即后三位毫秒为0)
moment转时间戳:moment(
time
).valueOf();
// 1535419062126
moment转时间:moment(timestamp).format();
// 2018-08-28T09:17:42+08:00
举例时间:var time = new Date(); //Tue Aug 28 2018 09:16:06 GMT+0800 (中国标准时间)时间戳:var timestamp = Date.parse(time); //1535419062000 (Date.parse() 默认不取毫秒,即后三位毫秒为0)moment转时间:moment(time).valueOf(); //...
moment
().format('X') // (大写X)以秒为单位,返回值为字符串类型
moment
().format('x') // (小写x)以毫秒为单位,返回值为字符串类型
时间戳
转换
成
时间
moment
(
时间戳
).format("YYY
moment
().format('X') // (大写X)以秒为单位,返回值为字符串类型
moment
().format('x') // (小写x)以毫秒为单位,返回值为字符串类型
moment
().valueOf() //
moment
().startOf('day').format('YYYY-MM-DD HH:mm:ss') // 当天0点的
时间
格式
moment
().startOf('day').format('X') // 当天0点的
时间
缀,以10位Unix
时间戳
输出(秒)
moment
().endOf('day').format('YYYY-MM-DD HH:mm:ss') // 当天23点59分59秒的
时间
格式
moment
().endOf('day').format('x') //当天23点59分...
Moment
.
js
是一个
JavaScript
日期处理类库(处理
时间
格式化的npm包),用于解析、检验、操作、以及显示日期,在新公司的项目中,大量使用
Moment
来处理
时间
日期,非常方便好用。
Moment
.
js
中文网:
http://
moment
js
.cn/
不依赖任何第三方库
支持字符串、Date、
时间戳
以及数组等格式
可以同时在浏览器和node环境...
通过后端接口发现接口返回的字段是 beginTime: “1612922019029”
在数据传递得过程中类型发生了变化 所以得把字符串用Number()
转
化下数字类型即可正常显示
//
转换
数字类型
beginTime: this.$
moment
(Number(item.beginTime)).format(
"YYYY-MM-DD"
还有个小问题就是如果某个
时间戳
为空是时,则会显示1970-01-01
所以为了防止显示
在
JavaScript
中,可以使用如下两个方法将
时间戳
转换
为
时间
:
1. `new Date(timestamp)`: 将
时间戳
作为参数传入 `Date` 构造函数,就可以得到一个
时间
对象。例如:
const timestamp = 1609459200000;
const date = new Date(timestamp);
console.log(date); // Mon Dec 21 2020 00:00:00 GMT+0800 (中国标准
时间
)
2. `Date.parse(dateString)`: 将
时间
字符串作为参数传入 `Date.parse` 方法,就可以得到
时间戳
。例如:
const dateString = "Mon Dec 21 2020 00:00:00 GMT+0800";
const timestamp = Date.parse(dateString);
console.log(timestamp); // 1609459200000
与此相反,可以使用如下方法将
时间
转换
为
时间戳
:
1. `Date.getTime()`: 将
时间
对象作为参数传入 `getTime` 方法,就可以得到
时间戳
。例如:
const date = new Date();
const timestamp = date.getTime();
console.log(timestamp); // 1609527540379
2. `Date.parse(dateString)`: 将
时间
字符串作为参数传入 `Date.parse` 方法,就可以得到
时间戳
。例如:
const dateString = "Mon Dec 21 2020 00:00:00 GMT+0800";
const timestamp = Date.parse(dateString);
console.log(timestamp); // 1609459200000