In every programming language there are tips, tricks and shortcuts to decrease effort when coding and increase work efficacy. Certain functions or expressions prepared in advance, for instance, such mathematical constants as epsilon or Pi have already been initialized, functions to convert variables or to get their values are in place, and so on.
Of course, programmers often have to work with dates and there are several things and expressions in JS designed to save time and effort, as well as make code volume lower.
Date object
In JavaScript, the date object works with everything related to date and time. In addition to the fact that you can work with the Date object itself, it contains methods that can deal with dates too. Date objects can be used to store, create, change, measure time or display the current date.
Okay, syntax of data object looks as follows:
new Date(); //will create an object with the current date and time
new Date(value);
new Date(dateString);
new Date(year, month, day, hour, minute, second, millisecond);
The logic of the Date
is thought out to the smallest detail - even if you specify incorrect values for a field, the Date
constructor will automatically convert them to the correct ones (though the date can be significantly changed).
For example, if specifying the number of minutes you specify 100 (while the maximum number of minutes is 59), the constructor will add one hour to the field with hours, and the value 41 will remain in minutes (100-59). If it will be difficult to correct the date, the method will still cope with it.
new Date(2000, 01, 01, 01, 100, 10); // 2000-01-01T02:41:10
The most useful method in practice is Data.now()
. It is easy to guess that it returns the actual time.
It is impossible to create time in hours only, it must be dated (year, month, day). Among the Date
methods we can distinguish two groups such as Set and Get methods. These groups of methods include ways to access the various components of the date and time of the Date
object together or separately.
Date.prototype Set methods
The following methods from the Set group allow you to set the date and time components comprehensively or individually to change one value:
setFullYear
(year [, month, date]);setMonth
(month [, date]);setDate
(date);setHours
(hour [, min, sec, ms]);setMinutes
(min [, sec, ms]);setSeconds
(sec [, ms]);setMilliseconds
(ms);setTime
(milliseconds).
Indicated in square brackets are optional parameters. As you see, some methods can set multiple date components at once. In this case, if a component is not specified, it does not change.
const x = new Date();
x.setHours(12); // specify only the hours
x.setHours(12, 12, 12, 12); // specify only hours, minutes, seconds, milliseconds
Date.prototype Get methods
This group of methods deserve a bit more attention:
getFullYear()
- 4-digit year format. By the way, you should use the getFullYear() method instead of getYear(), because the latter returns two digits only.getMonth()
- month from 0 to 11 (countdown starts from zero);getDate()
- date number from 1 to 31;getDay()
- number of the day of the week (week starts on Sunday);getHours()
,getMinutes()
,getSeconds()
,getMilliseconds()
- hours, minutes, seconds or milliseconds.
It is important that all the methods listed above return a result for the local time zone.
Date.prototype.getDate()
Although the name of the method suggests that it returns the full date, in fact this method returns only the day of the month of the specified date local time. Іn terms of syntax and parameters, this method does not accept parameters and looks like this:
x.getDate();
Аs a result, this method returns the day of the month, and this number ranges from 1 to 31.
const date_1 = new Date('October 5, 2000 12:00:00');
const date_2 = new Date('April 12');
const date_3 = new Date('April');
const date_4 = new Date(01,01,01,01,01,01);
const date_5 = new Date(10,10,10);
const date_6 = new Date(10,10);
date_1.getDate(); // 5 as a result
date_2.getDate(); // 12
date_3.getDate(); // NaN
date_4.getDate(); // 1
date_5.getDate(); // 10
date_6.getDate(); // 1 because in date6 day not initialize
Аnd now let's try to change the value of the day and see the result again.
const date = new Date(01,01,01);
date.getDate(); // 1 as a result
date.setDate(12);
date.getDate(); // 12 as a result
Or let's change the whole date:
const date = new Date('December 15, 2000 12:00:00');
date.getDate(); // 15 as a result
date.setFullYear(1990,04,36,12,12,21);
date.getDate(); // 5 as a result
By the way in this example, although we have specified the day of the month 36 (for April which has 31 days) as a result we will see the value 5, and the month will change to May. It is clear that if we specify completely unrealistic data, we will get NaN as a result.
const date = new Date(123456789,123456789,123456789);
date.getDate();
For all other methods you can intuitively guess how to work with them. This is because all these methods are concise, clear, and their application is boundless. By the way, arithmetic operations can also be used with Date
. For example, let's add a few days to a certain date.
const date = new Date("November 5, 2000 12:00:00");
date.setDate(date.getDate() + 12);
console.log(date); // Date Fri Nov 17 2000 12:00:00 GMT+0200 (Eastern European Standard Time)
Оr let's move to the past from the present date:
const date = new Date("November 5, 2000 12:00:00");
date.setDate(date.getDate() - 12);
console.log(date); // Date Tue Oct 24 2000 12:00:00 GMT+0300 (Eastern European Summer Time)