Home » JavaScript

Date setDate() method with example in JavaScript

JavaScript Date setDate() method: Here, we are going to learn about the setDate() method in JavaScript with example.
Submitted by IncludeHelp, on March 12, 2019

JavaScript Date setDate() method

setDate() method is a Date class method, it is used to set the current date (day of the month) to the Date object with a valid date value (between 1 to 31, based on the current month).

Note: Date larger than the last date of the month will be truncated from the starting date, for example, in the March there are 31 days and you set the date value to 33, then it will set to the date as the second day of the month (2 March).


    var dt = new Date();


    Input/Date class object declaration:
    var dt = new Date();

    Function call to set the date:

    Function call to get the date:


JavaScript code to demonstrate an example of Date.setDate() method

<head><title>JavaScipt Example</title></head>

	var dt = new Date();
	//getting current date (day of the month)
	var date = dt.getDate();
	document.write("date: " + date + "<br>");
	//setting date to 17
	//getting & printing date again
	date = dt.getDate();
	document.write("date: " + date + "<br>");
	//setting invalid date
	//setting date to 33 
	//This is March month, when I am writing this code
	//maximum month in March is 31, thus, 33 will set 
	//date as 2
	//getting & printing date again
	date = dt.getDate();
	document.write("date: " + date + "<br>");	


date: 12
date: 17
date: 2

Reference: JavaScript setDate() Method

Comments and Discussions

Ad: Are you a blogger? Join our Blogging forum.

Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.