Date object set methods in javascript

Dear friends, In this article, we will learn set methods of Date Object in javascript. These methods are used to put date value in Date Object in different ways.

Following examples will show all possible ways to use set methods of Date Object with their respective outputs.

1) setFullYear:

setFullYear method set the year (optionally month and day)

var d = new Date(); document.write(d+"<br />"); d.setFullYear(2020); document.write(d+"<br />");

OUTPUT: Thu Jun 20 2019 15:21:52 GMT+0530 (India Standard Time) Sat Jun 20 2020 15:21:52 GMT+0530 (India Standard Time)

2) setMonth:

setMonth method set the month (0-11) var d = new Date(); document.write(d+"<br />"); d.setMonth(11); document.write(d+"<br />");

OUTPUT: Thu Jun 20 2019 15:23:11 GMT+0530 (India Standard Time) Fri Dec 20 2019 15:23:11 GMT+0530 (India Standard Time)

3) setDate:

setDate method set the day as a number (1-31) var d = new Date(); document.write(d+"<br />"); d.setDate(26); document.write(d+"<br />");

OUTPUT: Thu Jun 20 2019 15:24:02 GMT+0530 (India Standard Time) Wed Jun 26 2019 15:24:02 GMT+0530 (India Standard Time)

4) setHours:

setHours method set the hour (0-23) var d = new Date(); document.write(d+"<br />"); d.setHours(19); document.write(d+"<br />");

OUTPUT: Thu Jun 20 2019 15:24:02 GMT+0530 (India Standard Time) Thu Jun 20 2019 19:24:45 GMT+0530 (India Standard Time)

5) setMinutes:

setMinutes method set the minutes (0-59) var d = new Date(); document.write(d+"<br />"); d.setMinutes(59); document.write(d+"<br />");

OUTPUT: Thu Jun 20 2019 15:24:02 GMT+0530 (India Standard Time) Thu Jun 20 2019 15:59:24 GMT+0530 (India Standard Time)

6) setSeconds:

setSeconds method set the seconds (0-59) var d = new Date(); document.write(d+"<br />"); d.setSeconds(59); document.write(d+"<br />");

OUTPUT: Thu Jun 20 2019 15:24:02 GMT+0530 (India Standard Time) Thu Jun 20 2019 15:25:59 GMT+0530 (India Standard Time)

7) setMilliseconds:

setMilliseconds method set the milliseconds (0-999) var d = new Date(); document.write(d+"<br />"); d.setMilliseconds(509); document.write(d+"<br />");

OUTPUT: Thu Jun 20 2019 15:24:02 GMT+0530 (India Standard Time) Thu Jun 20 2019 15:27:39 GMT+0530 (India Standard Time)

If you have any query or question or topic on which, we might have to write an article for your interest or any kind of suggestion regarding this post, Just feel free to write us, by hit add comment button below or contact via Contact Us form.


Your feedback and suggestions will be highly appreciated. Also try to leave comments from your valid verified email account, so that we can respond you quickly.

 
 

{{c.Content}}

Comment By: {{c.Author}}  On:   {{c.CreatedDate|date:'dd/MM/yyyy'}} / Reply


Categories