JavaScript set date methods; Through this tutorial, i am going to show you JavaScript set date methods with the help of examples.
Set Date Methods in JavaScript
There are set date methods in javaScript; as shown below:
- setFullYear()
- setMonth()
- setDate()
- setHours()
- setMinutes()
- setSeconds()
- setMilliseconds()
1 – The setFullYear()
The JavaScript setFullYear() is an inbuilt function of JavaScript which is used to set a year into a date object.
The syntax of setFullYear() method; as shown below:
DateObj.setFullYear(set_year);
The example of setFullYear() method; as shown below:
var today = new Date(); document.write("Today: "+today); document.write("<br />"); today.setFullYear(today.getFullYear() - 1); document.write("1 Year Before: "+today);
2 – The setMonth()
The JavaScript setMonth() is an inbuilt function of JavaScript. which is used to set month into a date object.
The syntax of setMonth() method; as shown below:
DateObj.setMonth(set_month);
The example of setMonth() method; as shown below:
var d=new Date(); d.setMonth(0); document.write("Set Month: "+d);
3 – The setDate()
The JavaScript setDate() is an inbuilt function of JavaScript. which is used to sets the day of the month of this date object.
The syntax of setDate() method; as shown below:
DateObj.setDate(set_date);
The example of setDate() method; as shown below:
var d=new Date(); d.setDate(5); document.write("Set Date: "+d);
4 – The setHours() Method
The JavaScript setHours() is an inbuilt function of JavaScript. which is used to sets the hour of a date object.
The syntax of setHours() method; as shown below:
DateObj.setHours(set_hour);
The example of setHours() method; as shown below:
var d=new Date(); d.setHours(10); document.write("Set Hours: "+d);
5 – The setMinutes()
The JavaScript setMinutes() is an inbuilt function of JavaScript. which is used to set minute into a date object.
The syntax of setMinutes () method; as shown below:
DateObj.setMinutes(set_minutes);
The example of setMinutes () method; as shown below:
var d=new Date(); d.setMinutes(22); document.write("Set Minutes: "+d);
6 – The setSeconds()
The setSeconds() Method – sets the seconds of a date object. This is a inbuilt function of JavaScript.
The syntax of setSeconds () method; as shown below:
DateObj.setSeconds(set_seconds);
The example of setSeconds() method; as shown below:
var d=new Date(); d.setSeconds(10); document.write("Set Seconds: "+d);
7 – The setMilliseconds()
The setMilliseconds() Method – sets the milliseconds of a date object.
The syntax of setMilliseconds () method; as shown below:
DateObj.setMilliseconds(set_milliseconds);
The example of setMilliseconds () method; as shown below:
var d=new Date(); d.setMilliseconds(55); document.write("Set Milli Seconds: "+d);