js method to get time year, month, day, timestamp, etc.

var myDate = new Date();//获取系统当前时间

 1 myDate.getYear(); //获取当前年份(2位)
 2 myDate.getFullYear(); //获取完整的年份(4位,1970-????)
 3 myDate.getMonth(); //获取当前月份(0-11,0代表1月)
 4 myDate.getDate(); //获取当前日(1-31)
 5 myDate.getDay(); //获取当前星期X(0-6,0代表星期天)
 6 myDate.getTime(); //获取当前时间(从1970.1.1开始的毫秒数)
 7 myDate.getHours(); //获取当前小时数(0-23)
 8 myDate.getMinutes(); //获取当前分钟数(0-59)
 9 myDate.getSeconds(); //获取当前秒数(0-59)
10 myDate.getMilliseconds(); //获取当前毫秒数(0-999)
11 myDate.toLocaleDateString(); //获取当前日期
12 var mytime=myDate.toLocaleTimeString(); //获取当前时间
13 myDate.toLocaleString( ); //获取日期与时间

 

How to get the current timestamp in JS

1、var timestamp =Date.parse(new Date());

The result obtained: 1657876666000 Note: The result obtained here converts the last three digits (milliseconds) into 000 for display

2、var timestamp =(new Date()).valueOf();

Result: 1657876666

3、var timestamp=new Date().getTime();

Result: 1657876666

Call new Date() separately in js, such as console.log(new Date());

The displayed result is: Mar 31 10:10:43 UTC+0800 2012 The time in this format

But using new Date() to participate in the calculation will automatically convert to the number of milliseconds since 1970.1.1.

Guess you like

Origin blog.csdn.net/xiansibao/article/details/125807751