Loading Please Wait...

Logo Lynxsia IT Solutions

JavaScript Date Set Methods

JS Date Set Methods

Set Date methods let you set date values (years, months, days, hours, minutes, seconds, milliseconds) for a Date Object.

Date Set Methods

The set methods listed below are used to set date values for a date objects.

Method Description
setFullYear() Set the year (optionally month and day)
setMonth() Set the month (0-11)
setDate() Set the day as a number (1-31)
setHours() Set the hour (0-23)
setMinutes() Set minute (0-59)
setSeconds() Set seconds (0-59)
setMilliseconds() Set millisecond (0-999)
setTime() Set the time (milliseconds since January 1, 1970)
The setFullYear() Method

The setFullYear() method sets the year of a date object.

The setFullYear() method can optionally set month and day.

					 
        
          const dateTime1 = new Date();
          dateTime1.setFullYear(2023);

          const dateTime2 = new Date();
          dateTime2.setFullYear(2023, 05, 25);
        
      
The setMonth() Method

The setMonth() method sets the month of a date object (0-11).

					 
        
          const dateTime = new Date();
          dateTime.setMonth(10);
        
      
The setDate() Method

The setDate() method sets the day of a date object (1-31).

The setDate() method can also be used to add days to a date. If adding days shifts the month or year, the changes are handled automatically by the Date object.

					 
        
          const dateTime1 = new Date();
          dateTime1.setDate(10);

          const dateTime2 = new Date();
          dateTime2.setDate(dateTime2.getDate() + 45);
        
      
The setHours() Method

The setHours() method sets the hours of a date object (0-23).

					 
        
          const dateTime = new Date();
          dateTime.setHours(16);
        
      
The setMinutes() Method

The setMinutes() method sets the minutes of a date object (0-59)

					 
        
          const dateTime = new Date();
          dateTime.setMinutes(06);
        
      
The setSeconds() Method

The setSeconds() method sets the seconds of a date object (0-59).

					 
        
          const dateTime = new Date();
          dateTime.setSeconds(49);
        
      
The setMilliseconds() Method

The setMilliseconds() method sets the milliseconds of a date object (0-999).

					 
        
          const dateTime = new Date();
          dateTime.setMilliseconds(494);
        
      
The setTime() Method

The setTime() method sets a date and time by adding or subtracting a specified number of milliseconds to/from midnight January 1, 1970.

					 
        
          const dateTime = new Date();
          dateTime.setTime(1332403882588);
          dateTime.setTime(-1332403882588);
        
      
Compare Dates

Dates can easily be compared with date object.

					 
        
          let text = "";
          const today = new Date();
          const someday = new Date();
          someday.setFullYear(2023, 07, 10);

          if (someday > today) {
            text = "Today is before July 10, 2023.";
          } else {
            text = "Today is after July 10, 2023.";
          }
        
      

How you feel about this blog:

Share this blog on:

Report Us

If you find any error in the turtorials, or want to share your suggestion/feedback, feel free to send us email at: info@lynxsia.com

Contact Us
Ads
Logo
Lynxsia IT Solutions

We are concern with various development process like website design & development, E-commerce development, Software development, Application development, SMS & Bulk SMS Provider, PWA Development, and many more..

Kotwali Road, Chhiptehri, Banda, 210001, UP, India

Copyright © 2022, Lynxsia IT Solutions, All rights reserved