JS時間轉時間戳,時間戳轉時間。時間顯示模式。

2021-09-22 10:54:43 字數 2506 閱讀 2912

// 時間轉為時間戳

function

date2timestamp(datetime)

// 時間戳轉時間

function

timestamp2date(timestamp, mode) $/, ' ').replace(/年|月/g, "-").replace(/日/g, " ").replace(/上午/g, "").replace(/下午/g, "");

var date_arr = tt.split(" ");

if (mode == 3)

else

if (weekc >= 1)

else

if (dayc >= 1)

else

if (hourc >= 1)

else

if (minc >= 1) else

result = "剛剛";

return result;

}if (mode == 2) else

if (parseint(timestamp) > parseint(b_date - 24 * 60 * 60) && parseint(timestamp) < parseint(e_date - 24 * 60 * 60))

// 顯示週幾

var days = new

array("星期日","星期一","星期二","星期三","星期四","星期五","星期六");

var day = new

date(date_arr[0]).getday();

return days[day];}}

if (mode == 1)

return tt;

}return tt;

}// 1498806947 2017.6.30 15:15

// 1498720547 2017.6.29 15:15

// 1498634147 2017.6.28 15:15

// 1497942947 2017.6.20 15:15

console.log(timestamp2date('1498806947', 3));

console.log(timestamp2date('1498720547', 3));

console.log(timestamp2date('1498634147', 3));

console.log(timestamp2date('1497942947', 3));

console.log("-------------------------------");

console.log(timestamp2date('1498806947', 2));

console.log(timestamp2date('1498720547', 2));

console.log(timestamp2date('1498634147', 2));

console.log(timestamp2date('1497942947', 2));

console.log("-------------------------------");

console.log(timestamp2date('1498806947', 1));

console.log(timestamp2date('1498720547', 1));

console.log(timestamp2date('1498634147', 1));

console.log(timestamp2date('1497942947', 1));

console.log("-------------------------------");

console.log(timestamp2date('1498806947'));

console.log(timestamp2date('1498720547'));

console.log(timestamp2date('1498634147'));

console.log(timestamp2date('1497942947'));

1小時前

1天前2天前

1週前-------------------------------

15:15

昨天星期三

2017-06-20

-------------------------------

15:15

2017-06-29 15:15

2017-06-28 15:15

2017-06-20 15:15

-------------------------------

2017-06-30 15:15

2017-06-29 15:15

2017-06-28 15:15

2017-06-20 15:15

js對於時間戳處理不太便捷。需要自己計算處理。根據需要,顯示不同的時間模式。

js獲取時間戳 時間戳轉時間

1 var timestamp date.parse new date 精準到秒2 var timestamp new date valueof 精準到毫秒3.var timestamp new date gettime 精準到毫秒1 var a new date tolocaledatestrin...

時間轉時間戳

把現在時間轉成timestamp datetime gtm new datetime 1970,1,1 宣告乙個gtm時間出來 datetime utc datetime.utcnow.addhours 8 宣告乙個目前的時間 int timestamp convert.toint32 timesp...

時間轉時間戳

把現在時間轉成timestamp datetime gtm new datetime 1970,1,1 宣告乙個gtm時間出來 datetime utc datetime.utcnow.addhours 8 宣告乙個目前的時間 int timestamp convert.toint32 timesp...