This article mainly introduces js to format the current time into year-month-day hour: minute: second. It mainly uses the Date() object of js to format the current time of the system into year-month-day hour: minute: Seconds, friends in need can refer to it, I hope it can help everyone.
Use the Date() object of js to format the current system time into year-month-day hours: minutes: seconds. You can also define the format yourself. (I encountered this problem while working on a project. I originally wanted to use Baidu, but the results online were too confusing, so I wrote one myself)
The code is as follows:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Time</title> </head> <body> <script type="text/javascript"> var d=new Date(); var year=d.getFullYear(); var month=change(d.getMonth()+1); var day=change(d.getDate()); var hour=change(d.getHours()); var minute=change(d.getMinutes()); var second=change(d.getSeconds()); function change(t){ if(t<10){ return "0"+t; }else{ return t; } } var time=year+'-'+month+'-'+day+' '+hour+':'+minute+':'+second; document.write(time); </script> </body> </html>
Effect demonstration:
##Related recommendations:bootStrap time formatting operation
phpThe specific process of formatting time
Analysis of the method of time formatting in Grid in Yii 2.0
The above is the detailed content of Detailed explanation of js formatting the current time into year, month, day, hour, minute and second format. For more information, please follow other related articles on the PHP Chinese website!