Home > Web Front-end > JS Tutorial > body text

Detailed explanation of js formatting the current time into year, month, day, hour, minute and second format

小云云
Release: 2018-01-24 10:33:50
Original
7299 people have browsed it

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+&#39;-&#39;+month+&#39;-&#39;+day+&#39; &#39;+hour+&#39;:&#39;+minute+&#39;:&#39;+second;
  document.write(time);
</script>
</body>
</html>
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!