Home  >  Article  >  Web Front-end  >  Formatting time code sharing in js

Formatting time code sharing in js

零下一度
零下一度Original
2017-06-26 11:48:511083browse

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!

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