Home > Web Front-end > JS Tutorial > How to set the month in javascript

How to set the month in javascript

藏色散人
Release: 2021-03-31 11:50:20
Original
2185 people have browsed it

In JavaScript, you can set the month through the setMonth method. The syntax is "dateObject.setMonth(month,day)". The parameter month is a numerical value representing the month, and the value is between 0 and 11. .

How to set the month in javascript

The operating environment of this tutorial: windows7 system, javascript version 1.8.5, Dell G3 computer.

setMonth() method is used to set the month.

Syntax

dateObject.setMonth(month,day)
Copy after login

Parameters

month Required. A number representing the month, between 0 (January) and 11 (December).

day

Optional. A numeric value representing the day of the month, between 1 and 31 (in local time).

This parameter is not supported until EMCAScript is standardized.

Return value

Adjusted date expressed in milliseconds. Before ECMAScript was standardized, this method returned nothing.

Tips and Notes:

Note: This method is always used in conjunction with a Date object.

[Recommended learning: js basic tutorial]

Example

Example 1

In this example, we will set the month field of object d to 0 (January) through the setMonth() method:

<script type="text/javascript">
var d=new Date()
d.setMonth(0)
document.write(d)
</script>
Copy after login

Output:

Sun Jan 31 2021 11:47:22 GMT+0800 (中国标准时间)
Copy after login

Example 2

In this example, we will set the month field of object d to 0 (January) and the day field to 20 through the setMonth() method:

<script type="text/javascript">
var d=new Date()
d.setMonth(0,20)
document.write(d)
</script>
Copy after login

Output :

Wed Jan 20 2021 11:47:22 GMT+0800 (中国标准时间)
Copy after login

The above is the detailed content of How to set the month in javascript. 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