Home > Web Front-end > JS Tutorial > body text

How to convert time to timestamp in javascript

青灯夜游
Release: 2023-01-06 11:13:53
Original
6681 people have browsed it

Conversion method: 1. Use the "Date.parse(new Date())" statement; 2. Use the "(new Date()).valueOf()" statement; 3. Use "new Date() .getTime()"; 4. Use "Number(new Date())".

How to convert time to timestamp in javascript

The operating environment of this tutorial: windows7 system, javascript version 1.8.5, Dell G3 computer.

There are four methods for javascript to obtain timestamps, all of which further obtain the current timestamp by instantiating the time object new Date()

Method 1:

var timestamp1 = Date.parse(new Date()); // 不推荐这种办法,毫秒级别的数值被转化为000
console.log(timestamp1);
Copy after login

Method 2:

var timestamp2 = (new Date()).valueOf(); // 通过valueOf()函数返回指定对象的原始值获得准确的时间戳值
console.log(timestamp2);
Copy after login

Method 3:

var timestamp3 = new Date().getTime(); // 通过原型方法直接获得当前时间的毫秒值,准确
console.log(timestamp3);
Copy after login

Method 4:

var timetamp4 = Number(new Date()) ; //将时间转化为一个number类型的数值,即时间戳
console.log(timetamp4);
Copy after login

The print result is as follows:

How to convert time to timestamp in javascript

【 Recommended learning: javascript advanced tutorial

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

Related labels:
source:php.cn
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!