JavaScript Date method: setDate()
[this page | pdf | back links]
The setDate() method (when applied to a JavaScript
date) sets the
date variable’s day of month.
It
has the following syntax with the following parameters:
date.setDate(day)
Parameter
|
Required / Optional
|
Description
|
day
|
Required
|
Integer representing
day of month. Typically, will be in range 1 – 31. However, 0 will result in
last day of previous month, -1 the day before that etc., and e.g. 32 for a 30-day
month will be second day of following month
|
EXAMPLE:
HTML USED IN THIS EXAMPLE:
<!DOCTYPE html>
<html> <!-- Copyright (c) Nematrian Limited 2018 -->
<head>
<style>
table,th,tr,td {border: 1px solid black; border-collapse: collapse;}
</style>
</head>
<body>
<table>
<tr>
<th>Example</th>
<th>Resulting value of <code>x</code></th>
</tr>
<tr>
<td><code id="Example"></code></td>
<td><code id="Result"></code></td>
</tr>
</table>
<script>
var d = new Date(Date.now());
document.getElementById("Example").innerHTML =
'var d = new Date(Date.now());<br>' +
'var x = new Date(d.setDate(1)).toISOString();';
document.getElementById("Result").innerHTML =
new Date(d.setDate(1)).toISOString();
</script>
</body>
</html>
|
FUNCTION THAT MAY ASSIST IN TESTING WHETHER FEATURE IS SUPPORTED:
function isSupportedJavaScriptMethodDateSetDate() {
var d = new Date(Date.now()); return !!d.setDate;
} |
NAVIGATION LINKS
Contents | Prev | Next | JavaScript Dates