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!