JavaScript - date methods

revision:


list of date methods and description.

Date() : returns today's date and time.

getDate() : returns the day of the month for the specified date according to local time.

getDay() : returns the day of the week for the specified date according to local time.

getFullYear() : returns the year of the specified date according to local time.

getHours() : returns the hour in the specified date according to local time.

getMilliseconds() : returns the milliseconds in the specified date according to local time.

getMinutes() : returns the minutes in the specified date according to local time.

getMonth() : returns the month in the specified date according to local time.

getSeconds() : returns the seconds in the specified date according to local time.

getTime() : returns the numeric value of the specified date as the number of milliseconds since January 1, 1970, 00:00:00 UTC.

getTimezoneOffset() : returns the time-zone offset in minutes for the current locale.

getUTCDate() : returns the day (date) of the month in the specified date according to universal time.

getUTCDay() : returns the day of the week in the specified date according to universal time.

getUTCFullYear() : returns the year in the specified date according to universal time.

getUTCHours() : returns the hours in the specified date according to universal time.

getUTCMilliseconds() : returns the milliseconds in the specified date according to universal time.

getUTCMinutes() : returns the minutes in the specified date according to universal time.

getUTCMonth() : returns the month in the specified date according to universal time.

getUTCSeconds() : returns the seconds in the specified date according to universal time.

getYear() : deprecated - returns the year in the specified date according to local time. Use getFullYear instead.

setDate() : sets the day of the month for a specified date according to local time.

setFullYear() : sets the full year for a specified date according to local time.

setHours() : sets the hours for a specified date according to local time.

setMilliseconds() : sets the milliseconds for a specified date according to local time.

setMinutes() : sets the minutes for a specified date according to local time.

setMonth() : sets the month for a specified date according to local time.

setSeconds() : sets the seconds for a specified date according to local time.

setTime() : sets the Date object to the time represented by a number of milliseconds since January 1, 1970, 00:00:00 UTC.

setUTCDate() : sets the day of the month for a specified date according to universal time.

setUTCFullYear() : sets the full year for a specified date according to universal time.

setUTCHours() : sets the hour for a specified date according to universal time.

setUTCMilliseconds() : sets the milliseconds for a specified date according to universal time.

setUTCMinutes() : sets the minutes for a specified date according to universal time.

setUTCMonth() : sets the month for a specified date according to universal time.

setUTCSeconds() : sets the seconds for a specified date according to universal time.

setYear() : deprecated - sets the year for a specified date according to local time. Use setFullYear instead.

toDateString() : returns the "date" portion of the Date as a human-readable string.

toGMTString() : deprecated - converts a date to a string, using the Internet GMT conventions. Use toUTCString instead.

toLocaleDateString() : returns the "date" portion of the Date as a string, using the current locale's conventions.

toLocaleFormat() : converts a date to a string, using a format string.

toLocaleString() : converts a date to a string, using the current locale's conventions.

toLocaleTimeString() : returns the "time" portion of the Date as a string, using the current locale's conventions.

toSource() : returns a string representing the source for an equivalent Date object; you can use this value to create a new object.

toString() : returns a string representing the specified Date object.

toTimeString() : returns the "time" portion of the Date as a human-readable string.

toUTCString() : converts a date to a string, using the universal time convention.

valueOf() : returns the primitive value of a Date object.

date static methods are invoked through the Date() constructor

Date.parse() : parses a string representation of a date and time and returns the internal millisecond representation of that date.

Date.UTC() : returns the millisecond representation of the specified UTC date and time.

examples:

code:
            <div>
                <p class="spec" id="date1"></p>
                <p class="spec" id="date2"></p>
                <p class="spec" id="date3"></p>
                <p class="spec" id="date4"></p>
                <p class="spec" id="date5"></p>
                <p class="spec" id="date6"></p>
                <p class="spec" id="date7"></p>
                <p class="spec" id="date8"></p>
                <p class="spec" id="date9"></p>
                <p class="spec" id="date10"></p>
                <p class="spec" id="date11"></p>
                <p class="spec" id="date12"></p>
                <p class="spec" id="date13"></p>
                <p class="spec" id="date14"></p>
                <p class="spec" id="date15"></p>
                <p class="spec" id="date16"></p>
                <p class="spec" id="date17"></p>
                <p class="spec" id="date18"></p>
                <p class="spec" id="date19"></p>
                <p class="spec" id="date20"></p>
                <p class="spec" id="date21"></p>
                <p class="spec" id="date22"></p>
                <p class="spec" id="date23"></p>
               
            </div>
            <script>
                const time = new Date();
                document.getElementById("date1").innerHTML = "time() : " + time;
        
                const d = new Date();
                let text = d.constructor;
                document.getElementById("date2").innerHTML = "constuctor : " + text;
                let day = d.getDate();
                document.getElementById("date3").innerHTML = "getDate : " + day;
                let day1 = d.getDay()
                document.getElementById("date4").innerHTML = "getDay : " + day1;
                let year = d.getFullYear();
                document.getElementById("date5").innerHTML = "getFullYear: " + year;
                let hour = d.getHours();
                document.getElementById("date6").innerHTML = "getHours: " + hour;
                let ms = d.getMilliseconds();
                document.getElementById("date7").innerHTML = "getMilliseconds : " + ms;
                let minutes = d.getMinutes();
                document.getElementById("date8").innerHTML = "getMinutes : " + minutes;
                let month = d.getMonth();
                document.getElementById("date9").innerHTML = "getMonth : " + month;
                let seconds = d.getSeconds();
                document.getElementById("date10").innerHTML = "getSeconds : " + seconds;
                let time1 = d.getTime();
                document.getElementById("date11").innerHTML = "getTime : " + time1;
                let diff = d.getTimezoneOffset();
                document.getElementById("date12").innerHTML =  "getTimezoneOffset: " + diff;
                let day2 = d.getUTCDate();
                document.getElementById("date13").innerHTML = "getUTCDate : " + day2;
                let day3 = d.getUTCDay();
                document.getElementById("date14").innerHTML = "getUTCDay : " + day3;
                let year2 = d.getUTCFullYear();
                document.getElementById("date15").innerHTML = "getUTCFullYear : " + year2;
                let hour2 = d.getUTCHours();
                document.getElementById("date16").innerHTML = "getUTCHours : " + hour2;
                let ms2 = d.getUTCMilliseconds();
                document.getElementById("date17").innerHTML = "geUTCtMilliseconds : " + ms2;
                let minutes2 = d.getUTCMinutes();
                document.getElementById("date18").innerHTML = "getUTCMinutes : " + minutes2;
                let month2 = d.getUTCMonth();
                document.getElementById("date19").innerHTML = "getUTCMonth : " + month2;
                let seconds2 = d.getUTCSeconds();
                document.getElementById("date20").innerHTML = "getUTCSeconds : " + seconds2;
                let ms3 = Date.now();
                document.getElementById("date21").innerHTML = "Date.now : " + ms3;
                const dd= new Date();
                dd.setDate(15);
                document.getElementById("date22").innerHTML =  "setDate : " + dd;
                dd.setFullYear(2024)
                document.getElementById("date23").innerHTML = " setFullYear: " + dd;
                
            </script>