JavaScript Date Methods: Get, Set, and Compare Dates
Learn how to pull the month, day, hour, and minute out of a JavaScript Date object, change them with setter methods, add days, and compare two dates.

Contents

Javascript Date Operations
javascript date(), that is the date function, can be used for many purposes inside an application. Let’s examine javascript date operations in detail, in line with your intended use cases. Among the functions that let you get the currently valid date values — such as the day of the month, the hour, the second and the minute — the most commonly used ones are listed below.
- getDate(): Returns the day-of-month value of the moment the function runs. (like 01-31)
- getDay(): Returns the day-of-week value of the current date (0-6: 0:Sunday,….,6:Saturday)
- getFullYear(): Returns the year value of the moment the function runs as a 4-digit number. (2021)
- getHours(): Returns the hour value. (0-23).
- getMilliseconds(): Returns the millisecond value. (0-999).
- getMinutes(): Returns the minute value. (0-59).
- getMonth(): Returns the month value (0-11).
- getSeconds(): Returns the second value (0-59).
- getTime(): Works from January 1, 1970 onward and returns a value in milliseconds.
Syntax:
After the new date() object is created, the functions are used as you like. However, the following forms can also be used when creating the date object.
- new Date( ) : If the object is created this way, an object is created based on the current date value.
- new Date(milliseconds) : An object is created that returns a value in milliseconds, counted from January 1, 1970.
- new Date(datestring) : If an object is created this way, it will use the value as a string, that is, in text form.
- new Date(year,month,date[hour,minute,second,millisecond ]) : It will use the values of the 7 parameters entered.
Now let’s try to understand it more closely with some simple examples below.
setFullYear
The setFullYear function changes the YEAR value in the given date and returns the new result.
In the example below, February 9, 2021 is used as the date.
var tarih = new Date(); // a date object was created and assigned to the tarih variable.
tarih.setFullYear(2021);
alert(tarih);
When the example above is run, it will return the newly created date value as 2021.
setDate
The setDate function changes the Day value in the given date and returns the new result.
var tarih = new Date(); // a date object was created and assigned to the tarih variable.
tarih.setDate(21);
alert(tarih);
How Do You Add Days to a Date You Choose?
You saw in the example above that it has a use like that, but you can also use the same function to add days to a date.
In the example below we will add 40 days to the current date.
var tarih = new Date();
tarih.setDate(tarih.getDate() + 40);
alert(tarih);
How Do We Compare Two Different Dates?
As in the example below, with javascript if else you can compare two different dates or carry out different operations.
var bugun, tarih;
bugun = new Date();
tarih = new Date();
tarih.setFullYear(2023);
if(tarih > bugun){
alert("Henüz 2025 yılına ulaşmadık.");
}
else
{
alert("Evet artık 2025 yılındayız.");
}
Share it if it helped


