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 does JavaScript convert numbers into time

2025-04-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)05/31 Report--

This article mainly introduces "how JavaScript converts numbers into time". In daily operation, I believe many people have doubts about how JavaScript converts numbers into time. The editor consulted all kinds of materials and sorted out simple and easy-to-use methods of operation. I hope it will be helpful to answer the doubts of "how JavaScript converts numbers into time". Next, please follow the editor to study!

The way JavaScript converts numbers into time: first encapsulate the string with a date, then encapsulate the conversion function, and finally call the function "date.Format (" yyyy-MM-dd hh:mm:ss ");".

This article operating environment: windows7 system, javascript1.8.5 version, DELL G3 computer

How does JavaScript convert numbers into time?

Js converts a string of numbers 1403149534 to date format

The fields in js that receive the date type in the json string returned by the background become a string of numbers, such as 1500341149000. So we need to format this string, such as: 2017-07-18 09:25:49.

Go directly to the code:

1. Encapsulate the string with date var date = new Date (1500341149000)

2. Encapsulate the conversion function

Date.prototype.Format = function (fmt) {/ / author:wangweizhen var o = {"M +": this.getMonth () + 1, / / month "d +": this.getDate (), / / day "h +": this.getHours (), / / hour "m +": this.getMinutes () / / minute "s +": this.getSeconds (), / / second "Q +": Math.floor ((this.getMonth () + 3) / 3), / / quarterly "S": this.getMilliseconds () / / millisecond} If (/ (y +) / .test (fmt)) fmt=fmt.replace (RegExp.$1, (this.getFullYear () + "). Substr (4-RegExp.$1.length)); for (var k in o) if (new RegExp (" ("+ k +")) .test (fmt)) fmt=fmt.replace (RegExp.$1, (RegExp.$1.length==1)? (O [k]): (("00" + o [k]). Substr (("" + o [k]) .length)); return fmt;}

3. Call function format: date.Format ("yyyy-MM-dd hh:mm:ss")

In this way, we can complete the conversion, dedicated to the friends who need it, this is the foreground formatting, so can we do some operations before converting json to get the format we want, that is, the conversion operation is done in the background.

At this point, the study on "how JavaScript converts numbers into time" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

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: 268

*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

Internet Technology

Wechat

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

12
Report