Vue如何將時間戳轉(zhuǎn)換日期格式
更新時間:2023年09月21日 15:38:55 作者:LW0512
這篇文章主要介紹了Vue如何將時間戳轉(zhuǎn)換日期格式,本文通過示例代碼給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
vue時間戳轉(zhuǎn)換日期格式
一,vue獲取時間戳轉(zhuǎn)換為日期格式
后臺返回的時間戳格式(例如:creatTime: 1626832597790),需要用時間格式顯示
(1)需要2021-09-05格式顯示

<el-table-column align="center" label="發(fā)布日期">
<template slot-scope="scope">
<span v-if="scope.row.creatTime != null">
{{ parseTime(scope.row.creatTime, "{y}-{m}-vvxyksv9kd") }}
</span>
</template>
</el-table-column>(2)需要2021-08-27 09:19:35格式顯示

<el-table-column align="center" label="提交反饋時間">
<template slot-scope="scope">
<span v-if="scope.row.creatTimes!= null">
{{ parseTime(scope.row.creatTime ) }}
</span>
</template>
</el-table-column>二, 需要向后臺傳時間戳格式的寫法 如下格式
(1)2020-09-28格式轉(zhuǎn)時間戳

return{
form:{
startTime:"",
endTime:"",
}
}startTime:new Date(this.form.startTime).getTime() endTime: new Date(this.form.endTime).getTime()
(2)如果開始時間或者結(jié)束時間取當(dāng)天時間
return{
form:{
startTime: new Date(),
endTime:"",
}
}startTime: new Date(this.form.startTime).getTime() endTime: new Date(this.form.endTime).getTime()
(3)如下格式 2021-09-28—2021-09-30格式

<el-form-item>
<span class="demonstration">日期篩選:</span>
<el-date-picker
v-model="createTime"
type="daterange"
range-separator="至"
start-placeholder="開始日期"
end-placeholder="結(jié)束日期"
>
</el-date-picker>
</el-form-item> return{
createTime:"",
}startTime:this.createTime && this.createTime[0] ? new Date(this.createTime[0]).getTime() : "", endTime:this.createTime && this.createTime[1] ? new Date(this.createTime[1]).getTime(): "",
三,獲取當(dāng)前的年月日時分秒并展示
<div class="rightime">
<div class="span1">{{ nowtime }}</div >
</div> return{
nowtime:""
}
mounted(){
setInterval(() => {
this.getTime();
}, 1000);
},
methods:{
getTime() {
this.nowtime = parseTime(new Date(), '{y}年{m}月vvxyksv9kd日 {h}:{i}:{s} 周{a}');
},
}
四,需要傳(2021-12-16)
<el-date-picker type="date" placeholder="選擇日期" v-model="auditorPostponeTime"> </el-date-picker>
data(){
return{
auditorPostponeTime:'',
}
}
let times = '';
if (this.auditorPostponeTime) {
times = parseTime(this.auditorPostponeTime, '{y}-{m}-vvxyksv9kd');
}
let req={
auditorPostponeTime: times, //同意選擇的時間
}五,注意:代碼中必須要引入date.js文件,并在方法中使用即可,否則以上不成立
import { parseTime } from "@/utils/date";(1)創(chuàng)建一個date.js文件,內(nèi)容如下:
/**
* Parse the time to string
* @param {(Object|string|number)} time
* @param {string} cFormat
* @returns {string | null}
*/
export function parseTime(time, cFormat) {
if (arguments.length === 0) {
return null
}
const format = cFormat || '{y}-{m}-vvxyksv9kd {h}:{i}:{s}'
let date
if (typeof time === 'object') {
date = time
} else {
if ((typeof time === 'string') && (/^[0-9]+$/.test(time))) {
time = parseInt(time)
}
if ((typeof time === 'number') && (time.toString().length === 10)) {
time = time * 1000
}
date = new Date(time)
}
const formatObj = {
y: date.getFullYear(),
m: date.getMonth() + 1,
d: date.getDate(),
h: date.getHours(),
i: date.getMinutes(),
s: date.getSeconds(),
a: date.getDay()
}
const time_str = format.replace(/{([ymdhisa])+}/g, (result, key) => {
const value = formatObj[key]
// Note: getDay() returns 0 on Sunday
if (key === 'a') { return ['日', '一', '二', '三', '四', '五', '六'][value] }
return value.toString().padStart(2, '0')
})
return time_str
}
/**
* @param {number} time
* @param {string} option
* @returns {string}
*/
export function formatTime(time, option) {
if (('' + time).length === 10) {
time = parseInt(time) * 1000
} else {
time = +time
}
const d = new Date(time)
const now = Date.now()
const diff = (now - d) / 1000
if (diff < 30) {
return '剛剛'
} else if (diff < 3600) {
// less 1 hour
return Math.ceil(diff / 60) + '分鐘前'
} else if (diff < 3600 * 24) {
return Math.ceil(diff / 3600) + '小時前'
} else if (diff < 3600 * 24 * 2) {
return '1天前'
}
if (option) {
return parseTime(time, option)
} else {
return (
d.getMonth() +
1 +
'月' +
d.getDate() +
'日' +
d.getHours() +
'時' +
d.getMinutes() +
'分'
)
}
}
/**
* @param {string} url
* @returns {Object}
*/
export function param2Obj(url) {
const search = url.split('?')[1]
if (!search) {
return {}
}
return JSON.parse(
'{"' +
decodeURIComponent(search)
.replace(/"/g, '\\"')
.replace(/&/g, '","')
.replace(/=/g, '":"')
.replace(/\+/g, ' ') +
'"}'
)
}到此這篇關(guān)于Vue時間戳轉(zhuǎn)換日期格式的文章就介紹到這了,更多相關(guān)Vue時間戳轉(zhuǎn)換日期格式內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Vue使用xlsx和xlsx-style導(dǎo)出表格出現(xiàn)部分樣式缺失的問題解決
這篇文章主要為大家詳細介紹一下Vue使用xlsx-style導(dǎo)出excel時樣式的設(shè)置,以及出現(xiàn)添加背景色,合并單元格部分樣式缺失問題的解決,需要的可以參考下2024-01-01

