Home  >  Article  >  Web Front-end  >  How to convert javascript date format to year, month and day

How to convert javascript date format to year, month and day

醉折花枝作酒筹
醉折花枝作酒筹Original
2021-04-08 17:11:3913189browse

JS date format conversion method: 1. Use "new Date()" to get the current date; 2. Use getFullYear() to get the year corresponding to the date, use "getMonth() 1" to get the corresponding month, use getDate() gets the date of the corresponding month.

How to convert javascript date format to year, month and day

The operating environment of this tutorial: Windows 7 system, JavaScript version 1.8.5, Dell G3 computer.

1. Get the current date.

<script type="text/javascript">
var d = new Date();
alert("当前日期: :" + d);
</script>

This date is not suitable for direct use and needs to be converted into an intuitive time.

How to convert javascript date format to year, month and day

2. Modified code:

var d = new Date();
var d2 = d.getFullYear() + "年"+ (d.getMonth() + 1) +"月"+ d.getDate() +"日";
alert("当前日期: :" + d2);


<img src="https://img.php.cn/upload/image/353/231/963/1617871135393328.png" title="1617871135393328.png" alt="How to convert javascript date format to year, month and day">

##[Recommended learning:

javascript video tutorial]

The above is the detailed content of How to convert javascript date format to year, month and day. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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
Previous article:how to write javascriptNext article:how to write javascript