- Demand Description: Write a function of the output of the current time, the format of yyyy-mm-dd hh: mm : ss
- Realization of ideas:
- Method One: First get the time of day, then call toLocaleString method, the time of day to convert to a string. Then replace method to replace the desired format.
- Method two: First, get the current time, then getFullYear, getMonth, getDate, getHours, getMinutes, getSeconds methods, respectively, acquires the year, month, day, hour, and then according to the desired format string to be spliced into
- Implementation code:
method one:
<! DOCTYPE HTML > < HTML lang = "EN" > < head > < Meta charset = "UTF-. 8" > < title > output current date and time format specified </ title > </ head > < body > < Script type = "text / JavaScript" > function getDate () { the let Today = new new a Date (); // the console.log (Today): Wed Jul-2019 17:48. 17:53 GMT + 0800 (China Standard Time) the let DATE = today.toLocaleString().replace(/下午/, " "); // console.log(today.toLocaleString()): 2019/7/17 下午5:48:53 return date; } document.write(getDate()); // date: 2019/7/17 5:48:53 </script> </body> </html>
Method Two:
<! DOCTYPE HTML > < HTML lang = "EN" > < head > < Meta charset = "UTF-. 8" > < title > output current date and time format specified </ title > </ head > < body > < Script type = "text / JavaScript" > function getDate () { the let Today = new new a Date (); // the console.log (Today): Wed Jul-2019 17:48. 17:53 GMT + 0800 (China Standard Time) the let newDate =today.getFullYear () + ' / ' + (today.getMonth () + . 1 ) + ' / ' + today.getDate () + ' ' + today.getHours () + ' : ' + today.getMinutes () + ' : ' + today.getSeconds (); // the getMonth method returns a number between 0-11, representing 1--12 month, calendar month and to corresponds to the time required +1. return newDate; } document.write (getDate ()); </ Script > </ body > </html>
- Renderings: