JS如何將當(dāng)前日期或指定日期轉(zhuǎn)時(shí)間戳
js將當(dāng)前日期或指定日期轉(zhuǎn)時(shí)間戳超詳細(xì)
獲取當(dāng)前時(shí)間并轉(zhuǎn)化成時(shí)間戳
var date = new Date() console.log(date) // Tue Aug 16 2022 14:52:09 GMT+0800 (中國(guó)標(biāo)準(zhǔn)時(shí)間) var timeStamp = Date.parse(date) console.log(timeStamp) // 1660632697000
轉(zhuǎn)化指定'年:月'為時(shí)間戳,日會(huì)默認(rèn)取1號(hào)0點(diǎn)0分
var date = '2022-8' var timeStamp = Date.parse(date) console.log(timeStamp) // 1659283200000 轉(zhuǎn)回去是2022-08-01 00:00:00
轉(zhuǎn)化指定'年:月:日'為時(shí)間戳,時(shí)間默認(rèn)0點(diǎn)0分
var date = '2022-8-16' var timeStamp = Date.parse(date) console.log(timeStamp) // 1660579200000 轉(zhuǎn)回去是2022-08-16 00:00:00
轉(zhuǎn)化指定'年:月:日 時(shí):分'為時(shí)間戳,秒默認(rèn)為0
var date = '2022-8-16 12:10' var timeStamp = Date.parse(date) console.log(timeStamp) // 1660623000000 轉(zhuǎn)回去是2022-08-16 12:10:00
轉(zhuǎn)化指定'年-月-日 時(shí):分:秒'為時(shí)間戳
var date = '2022-8-16 12:10:30' var timeStamp = Date.parse(date) console.log(timeStamp) // 1660623000000 轉(zhuǎn)回去是2022-08-16 12:10:30
獲取當(dāng)前年月日時(shí)分秒并轉(zhuǎn)化為時(shí)間戳,三種方式(標(biāo)準(zhǔn)時(shí)間轉(zhuǎn)時(shí)間戳)
let date = new Date()//獲取標(biāo)準(zhǔn)時(shí)間 //Tue Aug 16 2022 15:06:24 GMT+0800 (中國(guó)標(biāo)準(zhǔn)時(shí)間) //方式一 let timeStamp = date.getTime()//轉(zhuǎn)時(shí)間戳 //方式二 let timeStamp = date.valueOf() //方式三 let timeStamp = Date.parse(date) console.log(timeStamp) // 1606705009000
JS時(shí)間戳轉(zhuǎn)換方式示例詳解
前言
在js中將時(shí)間戳轉(zhuǎn)換為常用的時(shí)間格式,有三種主要的方式
1、使用JS中已有的函數(shù),例如getFullYear()
,getMonth()
等,將時(shí)間戳直接轉(zhuǎn)換成對(duì)應(yīng)的年月;
2、創(chuàng)建時(shí)間過(guò)濾器,在其他的頁(yè)面中直接調(diào)用該過(guò)濾器,轉(zhuǎn)換時(shí)間戳;
3、使用day.js
,將時(shí)間戳轉(zhuǎn)換成常用的時(shí)間寫法
4、本文以vue2和vue3兩個(gè)后臺(tái)管理系統(tǒng)中的下單時(shí)間為例,將原本的時(shí)間戳轉(zhuǎn)換為年月日的形式,其中vue2使用js和element ui,vue3使用TS和element-plus
1、js 時(shí)間戳轉(zhuǎn)日期(可直接復(fù)制)
// 時(shí)間戳 let timestamp = 1662537367 // 此處時(shí)間戳以毫秒為單位 let date = new Date(parseInt(timestamp) * 1000); let Year = date.getFullYear(); let Moth = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1); let Day = (date.getDate() < 10 ? '0' + date.getDate() : date.getDate()); let Hour = (date.getHours() < 10 ? '0' + date.getHours() : date.getHours()); let Minute = (date.getMinutes() < 10 ? '0' + date.getMinutes() : date.getMinutes()); let Sechond = (date.getSeconds() < 10 ? '0' + date.getSeconds() : date.getSeconds()); let GMT = Year + '-' + Moth + '-' + Day + ' '+ Hour +':'+ Minute + ':' + Sechond; console.log(GMT) // 2022-09-07 15:56:07
附加
let nowTime = new Date().valueOf();//時(shí)間戳 console.log(nowTime) // 獲取當(dāng)前時(shí)間的時(shí)間戳
2、在main.js中創(chuàng)建過(guò)濾器
示例:后臺(tái)管理系統(tǒng),vue2 + JS + element ui
,將下單時(shí)間的時(shí)間戳轉(zhuǎn)換為年月日的形式
(1)main.js
中,創(chuàng)建過(guò)濾器將其掛載到vue上
注意:我這邊后臺(tái)返回的數(shù)據(jù)需要進(jìn)行單位換算,所以originVal * 1000,具體情況具體分析,不同單位的數(shù)據(jù)請(qǐng)自行調(diào)整
import Vue from 'vue' // 創(chuàng)建過(guò)濾器,將秒數(shù)過(guò)濾為年月日,時(shí)分秒,傳參值originVal為毫秒 Vue.filter('dateFormat', function(originVal){ // 先把傳參毫秒轉(zhuǎn)化為new Date() const dt = new Date(originVal * 1000) const y = dt.getFullYear() // 月份是從0開始,需要+1 // +''是把數(shù)字轉(zhuǎn)化為字符串,padStart(2,'0')是把字符串設(shè)置為2位數(shù),不足2位則在開頭加'0' const m = (dt.getMonth() + 1 + '').padStart(2, '0') const d = (dt.getDate() + '').padStart(2, '0') return `${y}-${m}-$vvxyksv9kd` })
(2)頁(yè)面中具體使用
<el-table :data="orderList" border stripe class="mt20"> <el-table-column label="下單時(shí)間" prop="create_time"> <template slot-scope="scope"> {{scope.row.create_time | dateFormat}} </template> </el-table-column> </el-table>
3、day.js(鏈接直達(dá))
(1)三種安裝方式任選其一
npm install dayjs cnpm install dayjs -S yarn add dayjs
(2)頁(yè)面中具體使用
示例:后臺(tái)管理系統(tǒng),vue3 + TS + element-plus
,將下單時(shí)間的時(shí)間戳轉(zhuǎn)換為年月日的形式
使用前:
使用后:
① html部分
npm install dayjs cnpm install dayjs -S yarn add dayjs
②獲取到的數(shù)據(jù)
③TS部分
對(duì)拿到的數(shù)據(jù)中的創(chuàng)建時(shí)間進(jìn)行轉(zhuǎn)換,其中dayjs()中攜帶需要轉(zhuǎn)換的時(shí)間戳參數(shù),format()中攜帶所期待轉(zhuǎn)換成的形式
// 引入 import { dayjs } from "element-plus"; interface IOrderList { order_number: string; // 訂單編號(hào) create_time: number; // 下單時(shí)間 } const orderList = reactive<IOrderList[]>([]); // 獲取訂單數(shù)據(jù) const getOrderList = async () => { orderList.length = 0; let orders = await ordersAPI(pageInfo.value); // 對(duì) orders.data.goods進(jìn)行遍歷,dayjs()中攜帶需要轉(zhuǎn)換的時(shí)間戳參數(shù),format()中攜帶所期待轉(zhuǎn)換成的形式 orders.data.goods.forEach((el: any) => { el.create_time = dayjs(el.create_time * 1000).format("YYYY-MM-DD"); }); orderList.push(...orders.data.goods); }; getOrderList();
到此這篇關(guān)于JS時(shí)間戳轉(zhuǎn)換方式的文章就介紹到這了,更多相關(guān)js時(shí)間戳轉(zhuǎn)換內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
詳解ES6 中的Object.assign()的用法實(shí)例代碼
這篇文章主要介紹了ES6 Object.assign()的用法及用途,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-01-01JS實(shí)現(xiàn)模擬百度搜索“2012世界末日”網(wǎng)頁(yè)地震撕裂效果代碼
這篇文章主要介紹了JS實(shí)現(xiàn)模擬百度搜索“2012世界末日”網(wǎng)頁(yè)地震撕裂效果代碼,引入第三方插件實(shí)現(xiàn)頁(yè)面的抖動(dòng)、撕裂及圖片等效果,需要的朋友可以參考下2015-10-10JavaScript內(nèi)置對(duì)象之Array的使用小結(jié)
這篇文章主要介紹了JavaScript內(nèi)置對(duì)象之Array的使用小結(jié),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-05-05javascript之典型高階函數(shù)應(yīng)用介紹二
在前一篇文章javascript之典型高階函數(shù)中主要實(shí)現(xiàn)了幾個(gè)典型的functional函數(shù),文章最后也提出了疑問(wèn),為啥那樣的實(shí)現(xiàn)與F#之類的函數(shù)式語(yǔ)言“不太一樣”呢?今天來(lái)試試更“函數(shù)式”的實(shí)現(xiàn)2013-01-01javascript基礎(chǔ)進(jìn)階_深入剖析執(zhí)行環(huán)境及作用域鏈
下面小編就為大家?guī)?lái)一篇javascript基礎(chǔ)進(jìn)階_深入剖析執(zhí)行環(huán)境及作用域鏈。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-09-09用javascript實(shí)現(xiàn)記錄來(lái)賓姓名的代碼
用javascript實(shí)現(xiàn)記錄來(lái)賓姓名的代碼...2007-03-03跟我學(xué)習(xí)javascript的函數(shù)調(diào)用和構(gòu)造函數(shù)調(diào)用
跟我學(xué)習(xí)javascript的函數(shù)和構(gòu)造函數(shù)調(diào)用,主要包括三方面內(nèi)容函數(shù)調(diào)用、方法調(diào)用以及構(gòu)造函數(shù)調(diào)用,想要了解這些內(nèi)容的朋友千萬(wàn)不要錯(cuò)過(guò)下面的內(nèi)容。2015-11-11KnockoutJS 3.X API 第四章之?dāng)?shù)據(jù)控制流component綁定
這篇文章主要介紹了KnockoutJS 3.X API 第四章之?dāng)?shù)據(jù)控制流component綁定的相關(guān)資料,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2016-10-10