elementui组件核心常用方法-时间选择器

919 阅读1分钟

时间选择器

elementui时间选择器默认给我们的值是标准中国时间

(2[Mon Aug 01 2022 00:00:00 GMT+0800 (中国标准时间), Wed Aug 03 2022 00:00:00 GMT+0800 (中国标准时间)]

但是更多场景下我们所需要的年月日时分秒或者时间戳,elementui也给了value-format属性让我们自己选择想要的样式。

时间戳

使用value-format="timestamp" 属性来获取以毫秒为单位的时间戳。

<el-date-picker 
v-model="value1" 
type="daterange" 
range-separator="至" 
start-placeholder="开始日期"
end-placeholder="结束日期" 

value-format="timestamp"   //看这里

>
</el-date-picker>
[1659283200000, 1659542400000]

要知道时间戳分为两种 一种是以秒为单位的时间戳,一种是以毫秒为单位的时间戳,两者的换算关系为1000也就是说可以将毫秒时间戳除1000:

value = [1659283200000, 1659542400000]
value[0]/1000      //1659283200

年月日时分秒

使用value-format="yyyy 年 MM 月 dd 日"属性来获取年月日类型的时间。

value-format="YYYY 年 MM 月 DD 日"
 ['2022 年 08 月 01 日', '2022 年 08 月 03 日']

当然也可以将年月日换成 - 或者其他字符。 如果需要时分秒:

value-format="yyyy 年 MM 月 dd 日 HH:mm:ss"
 ['2022 年 08 月 01 日 00:00:00', '2022 年 08 月 03 日 00:00:00']