JavaScript Set Date Methods

Set Date Methods

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

The Date object is a datatype built into the JavaScript language. Date objects are created with the new Date( ) as shown below.

Once a Date object is created, a number of methods allow you to operate on it. Most methods simply allow you to get and set the year, month, day, hour, minute, second, and millisecond fields of the object, using either local time or UTC (universal, or GMT) time.

The ECMAScript standard requires the Date object to be able to represent any date and time, to millisecond precision, within 100 million days before or after 1/1/1970. This is a range of plus or minus 273,785 years, so JavaScript can represent date and time till the year 275755.

You can use any of the following syntaxes to create a Date object using Date() constructor.

      
                    new Date( )
new Date(milliseconds)
new Date(datestring)
new Date(year,month,date[,hour,minute,second,millisecond ])
                    

No Argument: With no arguments, the Date() constructor creates a Date object set to the current date and time.

milliseconds: When one numeric argument is passed, it is taken as the internal numeric representation of the date in milliseconds, as returned by the getTime() method. For example, passing the argument 5000 creates a date that represents five seconds past midnight on 1/1/70.

datestring: When one string argument is passed, it is a string representation of a date, in the format accepted by the Date.parse() method.

7 agruments :To use the last form of the constructor shown above. Here is a description of each argument

year Integer value representing the year. For compatibility (in order to avoid the Y2K problem), you should always specify the year in full; use 1998, rather than 98.

month Integer value representing the month, beginning with 0 for January to 11 for December.

date Integer value representing the day of the month.

hour Integer value representing the hour of the day (24-hour scale).

minutes Integer value representing the minute segment of a time reading.

secound Integer value representing the second segment of a time reading.

millisecond Integer value representing the millisecond segment of a time reading.

The setFullYear() Method

The setFullYear() method sets the year of a date object. In this example to 2020:

      
                    var d = new Date();
d.setFullYear(2020);
document.getElementById("demo").innerHTML = d;
                    

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

      
                    var d = new Date();
d.setFullYear(2020, 11, 3);
document.getElementById("demo").innerHTML = d;
                    

The setMonth() Method

The setMonth() method sets the month of a date object (0-11): JavaScript identifiers are case-sensitive.

      
                    var d = new Date();
d.setMonth(11);
document.getElementById("demo").innerHTML = d;
                    

The setDate() Method

The setDate() method can also be used to add days to a date:

      
                    var d = new Date();
d.setDate(d.getDate() + 50);
document.getElementById("demo").innerHTML = d;
                    

If adding days shifts the month or year, the changes are handled automatically by the Date object.

The setHours() Method

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

      
                    var d = new Date();
d.setHours(22);
document.getElementById("demo").innerHTML = d;
                    

The setMinutes() Method

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

      
                    var d = new Date();
d.setMinutes(30);
document.getElementById("demo").innerHTML = d;
                    

The setSeconds() Method

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

      
                    var d = new Date();
d.setSeconds(30);
document.getElementById("demo").innerHTML = d;
                    
                    

Compare Dates

Dates can easily be compared. The following example compares today's date with January 14, 2100:

      
                    var today, someday, text;
today = new Date();
someday = new Date();
someday.setFullYear(2100, 0, 14);

if (someday > today) {
    text = "Today is before January 14, 2100.";
} else {
    text = "Today is after January 14, 2100.";
}
document.getElementById("demo").innerHTML = text;
                    

Dess App

DessApp is an Integrated E-learning Education, Interactive and User-friendly features, smarter options and redefining your school costs effectively and efficiently.

View
1 1