各種時間格式轉換 時間戳轉換

2022-06-28 17:00:13 字數 1953 閱讀 2036

1、後端介面返回時間格式轉換成時間戳

例:2021-02-15t09:33:08.694+0000

方案1:

const time = 2021-02-15t09:33:08.694+0000

時間戳:new date(time).gettime()

方案2: 安裝moment

import moment from 'moment';

const time = 2021-02-15t09:33:08.694+0000

時間戳:moment(time).valueof()

2、後端返回時間格式轉換成 展示的時間狀態

例如 2021-02-15t09:33:08.694+0000 =>2021-02-15 09:33:08

方案1:安裝moment

import moment from 'moment';

const time = 2021-02-15t09:33:08.694+0000

方案2:不展示

使用正規表示式分別找到't'、'.'的索引值,然後字串的方式擷取

3、時間戳展示成展示格式(2020-12-04 15:22:42)或者年月日

可直接引用此函式

gettsformatdate(timestamp)

if (strdate >= 0 && strdate <= 9)

if (hours >= 0 && hours <= 9)

if (minutes >= 0 && minutes <= 9)

if (seconds >= 0 && seconds <= 9)

var currentdate = `$-$-$ $:$:$`;

//或年月日 (注意展示年月日的時候上面的month、strdate、hours、minutes、seconds可不做加0處理)

//var currentdate = `$年$月$日 $時$分$秒`;

return currentdate;

}4、擴充套件

// 將當前時間換成時間格式字串

var timesta*** = 1403058804;

var newdate = new date();

newdate.settime(timesta*** * 1000);

// wed jun 18 2014

console.log(newdate.todatestring());

// wed, 18 jun 2014 02:33:24 gmt

console.log(newdate.togmtstring());

// 2014-06-18t02:33:24.000z

console.log(newdate.toisostring());

// 2014-06-18t02:33:24.000z

console.log(newdate.tojson());

// 2023年6月18日

console.log(newdate.tolocaledatestring());

// 2023年6月18日 上午10:33:24

console.log(newdate.tolocalestring());

// 上午10:33:24

console.log(newdate.tolocaletimestring());

// wed jun 18 2014 10:33:24 gmt+0800 (中國標準時間)

console.log(newdate.tostring());

// 10:33:24 gmt+0800 (中國標準時間)

console.log(newdate.totimestring());

// wed, 18 jun 2014 02:33:24 gmt

console.log(newdate.toutcstring());

時間格式轉換 時間戳

時間 附 dateformat parser new dateformat eeee,mmmm dd,yyyy dateformat formatter new dateformat eee.mm dd dateformat sdf new dateformat yyyy mm dd hh mm s...

時間格式轉換 時間戳的轉換

1 thu mar 07 2019 12 00 00 gmt 0800 中國標準時間 轉換為 2019 03 07 12 00 00 const d new date thu mar 07 2019 12 00 00 gmt 0800 中國標準時間 const resdate d.getfullye...

JS將時間戳轉換時間格式

var time entry.createtime 時間戳 13位字串 var createtime new date settime time 通過時間戳設定時間值 var date createtime.format yyyy mm dd hh mm ss 測試下這種方式 newcell8.in...