Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use new Date () in js

2025-01-15 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly introduces how to use new Date () in js, has a certain reference value, interested friends can refer to, I hope you can learn a lot after reading this article, the following let the editor take you to understand.

Js new Date () test

Var t = new Date (). ToString (); / t = "Thu Oct 31 2019 11:36:57 GMT+0800 (China Standard time)" var T1 = new Date (); var data_arry = []; var that = new Date (); data_arry.push (that.constructor ()); / / Thu Oct 31 2019 11:50:26 GMT+0800 (China Standard time) "data_arry.push (that.getDate ()); / / 31data_arry.push (that.getDay ()) / / 4data_arry.push (that.getFullYear ()); / / 2019data_arry.push (that.getHours ()); / / 11data_arry.push (that.getMilliseconds ()); / / 401data_arry.push (that.getMinutes ()); / / 50data_arry.push (that.getMonth ()); / / 9data_arry.push (that.getSeconds ()); / / 26data_arry.push (that.getTime ()); / / 1572493826401data_arry.push (that.getTimezoneOffset ()) / /-480data_arry.push (that.getUTCDate ()); / / 31data_arry.push (that.getUTCDay ()); data_arry.push (that.getUTCFullYear ()); data_arry.push (that.getUTCHours ()); data_arry.push (that.getUTCMilliseconds ()); data_arry.push (that.getUTCMinutes ()); data_arry.push (that.getUTCMonth ()); data_arry.push (that.getUTCSeconds ()); data_arry.push (that.getYear ()) Data_arry.push (that.setDate ()); data_arry.push (that.setFullYear ()); data_arry.push (that.setHours ()); data_arry.push (that.setMilliseconds ()); data_arry.push (that.setMinutes ()); data_arry.push (that.setMonth ()); data_arry.push (that.setSeconds ()); data_arry.push (that.setTime ()); data_arry.push (that.setUTCDate ()); data_arry.push (that.setUTCFullYear ()) Data_arry.push (that.setUTCHours ()); data_arry.push (that.setUTCMilliseconds ()); data_arry.push (that.setUTCMinutes ()); data_arry.push (that.setUTCMonth ()); data_arry.push (that.setUTCSeconds ()); data_arry.push (that.setYear ()); data_arry.push (that.toDateString ()); data_arry.push (that.toGMTString ()); / / data_arry.push (that.toISOString ()) Data_arry.push (that.toJSON ()); data_arry.push (that.toLocaleDateString ()); data_arry.push (that.toLocaleString ()); data_arry.push (that.toLocaleTimeString ()); data_arry.push (that.toString ()); data_arry.push (that.toTimeString ()); data_arry.push (that.toUTCString ()); data_arry.push (that.valueOf ())

Function

Function get_nowtime () {var date = new Date (); return (date.getFullYear () +'-'+ (date.getMonth () + 1) +'-'+ date.getDate ());} console.log (get_nowtime ())

Extended reading:

The method for JS to obtain the current timestamp-JavaScript to obtain the current timestamp

JavaScript gets the current timestamp:

The first method:

Var timestamp = Date.parse (new Date ())

Result: 1280977330000

The second method:

Var timestamp = (new Date ()) .valueOf ()

Results: 1280977330748

The third method:

Var timestamp=new Date () .getTime ()

Results: 1280977330748

The first: the acquired timestamp is changed from millisecond to 000 display.

The second and third are to get the current millisecond timestamp.

In the process of using js to show the approximate time left in the analysis data, my colleagues and I always changed the time to 0, and the result was very weird. finally, we found that we used the time stamp obtained by Date.parse (newDate ()) to change the millisecond to 000 display, so the time difference calculation was not accurate.

The second or third method can be used to calculate the time difference.

Call new Date () separately in js, for example, [xss_clean] (new Date ())

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

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

Thank you for reading this article carefully. I hope the article "how to use new Date () in js" shared by the editor will be helpful to everyone. At the same time, I also hope you will support us and pay attention to the industry information channel. More related knowledge is waiting for you to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report