WEB DEVELOPER SITE
HTMLCSSJAVASCRIPTSQLPHPBOOTSTRAPJQUERYANGULARXML
 

JavaScript Date Methods


Date techniques let you get and set date esteems (years, months, days, hours, minutes, seconds, milliseconds)


Date Get Methods

Get techniques are utilized for getting a piece of a date. Here are the most widely recognized (alphabetically):

Method Description
getDate() Get the day as a number (1-31)
getDay() Get the weekday as a number (0-6)
getFullYear() Get the four digit year (yyyy)
getHours() Get the hour (0-23)
getMilliseconds() Get the milliseconds (0-999)
getMinutes() Get the minutes (0-59)
getMonth() Get the month (0-11)
getSeconds() Get the seconds (0-59)
getTime() Get the time (milliseconds since January 1, 1970)

The getTime() Method

getTime() restores the quantity of milliseconds since January 1, 1970:

Example

<script>
var d = new Date();
document.getElementById("demo").innerHTML = d.getTime();
</script>
Try it Yourself »

The getFullYear() Method

getFullYear() restores the time of a date as a four digit number:

Example

<script>
var d = new Date();
document.getElementById("demo").innerHTML = d.getFullYear();
</script>
Try it Yourself »

The getDay() Method

getDay() restores the weekday as a number (0-6):

Example

<script>
var d = new Date();
document.getElementById("demo").innerHTML = d.getDay();
</script>
Try it Yourself »

You can utilize a variety of names, and getDay() to restore the weekday as a name:

Example

<script>
var d = new Date();
var days = ["Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"];
document.getElementById("demo").innerHTML = days[d.getDay()];
</script>
Try it Yourself »

Date Set Methods

Set strategies are utilized for setting a piece of a date. Here are the most well-known (alphabetically):

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

The setFullYear() Method

setFullYear() sets a date item to a particular date. In this model, to January 14, 2020:

Example

<script>
var d = new Date();
d.setFullYear(2020, 0, 14);
document.getElementById("demo").innerHTML = d;
</script>
Try it Yourself »

The setDate() Method

setDate() sets the day of the month (1-31):

Example

<script>
var d = new Date();
d.setDate(20);
document.getElementById("demo").innerHTML = d;
</script>
Try it Yourself »

The setDate() strategy can likewise be utilized to add days to a date:

Example

<script>
var d = new Date();
d.setDate(d.getDate() + 50);
document.getElementById("demo").innerHTML = d;
</script>
Try it Yourself »

Date Input - Parsing Dates

If you have a legitimate date string, you can utilize the Date.parse() strategy to change over it to milliseconds.

Date.parse() restores the quantity of milliseconds between the date and January 1, 1970:

Example

<script>
var msec = Date.parse("March 21, 2012");
document.getElementById("demo").innerHTML = msec;
</script>
Try it Yourself »

You would then be able to utilize the quantity of milliseconds to convert it to a date object:

Example

<script>
var msec = Date.parse("March 21, 2012");
var d = new Date(msec);
document.getElementById("demo").innerHTML = d;
</script>
Try it Yourself »

Compare Dates

Dates can without much of a stretch be compared.

The following model contrasts the present date and January 14, 2100:

Example

var today, sometime in the not so distant future, text;
today = new Date();
someday = new Date();
someday.setFullYear(2100, 0, 14);

if (some time or another > today) {
    content = "Today is before January 14, 2100.";
} else {
    content = "Today is after January 14, 2100.";
}
document.getElementById("demo").innerHTML = content;
Try it Yourself »

UTC Date Methods

UTC date techniques are utilized for working UTC dates (Univeral Time Zone dates):

Method Description
getUTCDate() Same as getDate(), yet restores the UTC date
getUTCDay() Same as getDay(), yet restores the UTC day
getUTCFullYear() Same as getFullYear(), however restores the UTC year
getUTCHours() Same as getHours(), however restores the UTC hour
getUTCMilliseconds() Same as getMilliseconds(), however restores the UTC milliseconds
getUTCMinutes() Same as getMinutes(), yet restores the UTC minutes
getUTCMonth() Same as getMonth(), however restores the UTC month
getUTCSeconds() Same as getSeconds(), however restores the UTC seconds