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

Formatting time code sharing in js

零下一度
Release: 2017-06-26 11:48:51
Original
1106 people have browsed it

In js, it is often required to format the output format of time, such as 2017-01-01 10:10. The more troublesome ones are month, day, hour, and minute. Their format generally requires two digits. If it is less than 10, you need to add 0 in front. Of course, this is not a problem and can be done through judgment, such as day = day>10?day:("0"+day); But this I write too much code and always want to write some innovative code.

var format= function(obj){

     return (obj/100).toFixed(2).split('.').pop();

   }

var time= new Date();

timeStr= time.getFullYear()+"-"+format(time.getMonths())+"-"+format(time.getDate ())+" "+ format(time.getHours())+":"format(time.getMinutes());

Although the code has not been reduced much, it is tidier. In my opinion

The above is the detailed content of Formatting time code sharing in js. 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