網站首頁 學習教育 IT科技 金融知識 旅遊規劃 生活小知識 家鄉美食 養生小知識 健身運動 美容百科 遊戲知識 綜合知識
當前位置:趣知科普吧 > IT科技 > 

js獲取時間戳

欄目: IT科技 / 發佈於: / 人氣:2.36W

JS中獲取時間戳可以透過Date.now()方法來實現,返回自1970年1月1日00:00:00UTC以來經過的毫秒數也就是當前時間戳。

具體方法如下:

輸入代碼:

<!DOCTYPE html>

<html>

<head>

<meta charset="utf-8">

<title>JS獲取當前時間戳的方法示例</title>

</head>

<body>

<script type="text/javascript">

//創建一個時間戳

var time = Date.now();

document.write(time + "<hr>");

</script>

</body>

</html>

獲取當前時間戳結果爲:1544516454900

js獲取時間戳

拓展資料:

時間戳是什麼?

時間戳是指格林威治時間1970年01月01日00時00分00秒(北京時間1970年01月01日08時00分00秒)起至現在的總毫秒數。

Date.now()方法返回自1970年1月1日00:00:00 UTC以來經過的毫秒數。

時間戳的語法:

var timeInMs = Date.now();

返回值是表示自UNIX紀元以來經過的毫秒數。

時間戳格式轉換爲中國標準時間格式:

js獲取時間戳 第2張

輸入代碼:

<!DOCTYPE html>

<html>

<head>

<meta charset="utf-8">

<title></title>

</head>

<body>

<script type="text/javascript">

//創建一個時間戳

var time = Date.now();

//document.write(time + "<hr>");

//將其轉換爲人們可讀的日期和時間

var d = new Date(time);

document.write(d);

</script>

</body>

</html>

Date 對象是用於處理日期和時間, new 關鍵詞用來定義 Date 對象。js中使用new Date()將時間戳轉換爲人們可讀的日期和時間,顯示的格式是:星期+月份+日+年+時分秒+時區。

Tags:js 時間 獲取