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

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

醉折花枝作酒筹
Release: 2021-04-08 17:11:39
Original
13188 people have browsed it

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>
Copy after login

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);
Copy after login


<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!

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