Date setMinutes() method with example in JavaScript

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

JavaScript Date setMinutes() method

setMinutes() method is a Date class method, it is used to set the minutes to the Date object with a valid minutes value (between 00 to 59).

Note: Minutes value larger than the 59 will be truncated from the starting minutes value (00), for example, we are going to set 61 as a minute value, it will be truncated as 1.

Syntax:

    var dt = new Date();
    dt.setMinutes(minute);

Examples:

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

    Function call to set the Minutes:
    dt.setMinutes(45);

    Function call to get the Minutes:
    dt.getMinutes(); 
    
    Output:
    45

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

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

<body>
<script>
	var dt = new Date();
	//getting current Minutes
	var Minute = dt.getMinutes();
	//printing
	document.write("Minute: " + Minute + "<br>");
	
	//setting Minute to 45
	dt.setMinutes(45);
	//getting & printing Minutes again
	Minute = dt.getMinutes();
	document.write("Minute: " + Minute + "<br>");	

	//setting invalid Minutes
	//setting Minute to 61
	dt.setMinutes(61);
	//getting & printing Minutes again
	Minute = dt.getMinutes();
	document.write("Minute: " + Minute + "<br>");		
</script>
</body>
</html>

Output

Minute: 2
Minute: 45
Minute: 1

Reference: JavaScript setMinutes() Method

JavaScript Date Object Methods »




Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.