Home Daily Programming HTML Knowledge How to get the current timestamp in JS

How to get the current timestamp in JS

Dec 12, 2018 pm 04:57 PM

JS can obtain the current timestamp through the Date.now() method, which returns the number of milliseconds that have passed since January 1, 1970 00:00:00 UTC, which is the current timestamp.

How to get the current timestamp in JS

# Now we will introduce how to get the current timestamp with JS based on specific code examples.

The code example is as follows:

<!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>
Copy after login

Get the current timestamp result as follows:

How to get the current timestamp in JS

The timestamp refers to Greenway The total number of milliseconds from 00:00:00 on January 1, 1970 (08:00:00 on January 1, 1970, Beijing time) to the present.

Date.now() method Returns the number of milliseconds that have elapsed since January 1, 1970 00:00:00 UTC.

The syntax:

var timeInMs = Date.now();
Copy after login

The return value represents the number of milliseconds that have passed since the UNIX epoch.

But the timestamp format should not be easy for most people to understand, so how do we convert it into a time format that everyone is familiar with? It's actually very simple.

The code example is as follows:

<!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>
Copy after login

The conversion result is as shown below:

How to get the current timestamp in JS

Date object is used to process date and time, new Keywords are used to define Date objects.

Use new Date() in js to convert the timestamp into a human-readable date and time. The format displayed is: week, month, day, year, hour, minute, second, time zone

This article is about JS An introduction to the method of obtaining the current timestamp and converting it to normal time format. It is very simple. I hope it will be helpful to friends in need!

The above is the detailed content of How to get the current timestamp in JS. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
1 months ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Chat Commands and How to Use Them
1 months ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)