1+X Web front-end (primary) exam knowledge points "js time formatting"

1+X Web front-end (primary) exam knowledge points "js time formatting"

1. Realize the effect

Get the current date through js and display it, as shown in the figure below:
insert image description here

2. Common methods of Date object

var date = new Date();
method the code
getFullYear() (year) to get the 4-digit year
getMonth()() (month) get the month
getDate() (day) get the day of the month
getHours() (hours) get 0 - 23 hours
getMinutes() (minutes) get minutes
getSeconds() (seconds) get seconds
getTime() (timestamp) milliseconds from 1970-01-01 to present

3. Complete code

<!DOCTYPE html>
<html>
  <head>
    <title>格式化日期</title>
    <meta charset="utf-8">
  </head>
  <body>
    
    <script type="text/javascript">
      
      var d = new Date();
      
      var year = d.getFullYear();
      var month = d.getMonth()+1;
      var day = d.getDate();
      var hour = d.getHours();
      var minute = d.getMinutes();
      var second = d.getSeconds();
      var week = d.getDay();
      
      var str = year+"年"+month+"月"+day+"日"+hour+":"+minute+":"+second;
      document.write("<h2 style='text-shadow:10px 10px 5px gray;'>"+str+"</h2>");
      
    </script>
    
  </body>
</html>


Guess you like

Origin blog.csdn.net/fujian87232/article/details/124901059