JavaScript Built-in objects

In addition to being able to create custom objects, JavaScript also provides a set of built-in object types that we can use in a variety of situations.

Date object: Working with dates

The Date object allows you to work with dates and times in JavaScript.

There are various ways to create a Date object. The first way is to use an empty constructor with no parameters:

var currentDate = new Date();
document.write(currentDate);
[js]

In this case, the object will point to the current date of the computer:

Creating a Date Object in JavaScript

The second way is to pass to the Date constructor the number of milliseconds that have passed since the beginning of the Unix epoch, i.e. since January 1, 1970 00:00:00 GMT:

[js]
var myDate = new Date(1359270000000);
document.write(myDate); // Sun Jan 27 2013 10:00:00 GMT+0300 (RTZ 2 (winter))

The third way is to pass the day, month, and year to the Date constructor:

var myDate = new Date("27 March 2008");
// or so
// var myDate = new Date("3/27/2008");
document.write(myDate); // Thu Mar 27 2008 00:00:00 GMT+0300 (RTZ 2 (winter))

If we use the full name of the month, then it is written in English, if we use the abbreviated version, then the month/day/year format is used.

The fourth way is to pass all date and time parameters to the Date constructor:

var myDate = new Date(2012,11,25,18,30,20,10); // Tue Dec 25 2012 18:30:20 GMT+0300 (RTZ 2 (winter))

In this case, the following parameters are used in order: new Date(year, month, day, hour, minutes, seconds, milliseconds). In this case, it should be borne in mind that the countdown of months starts from zero, that is, January is 0, and December is 11.

Getting the date and time

A number of methods are used to obtain the various components of a date:

  • getDate() : returns the day of the month
  • getDay() : returns the day of the week (counting starts from 0 – Sunday, and the last day is 6 – Saturday)
  • getMonth() : returns the number of the month (counting starts from zero, i.e. month number 0 is January)
  • getFullYear() : returns the year
  • toDateString() : returns the complete date as a string
  • getHours() : returns the hour (from 0 to 23)
  • getMinutes() : returns minutes (0 to 59)
  • getSeconds() : returns seconds (0 to 59)
  • getMilliseconds() : returns milliseconds (0 to 999)
  • toTimeString() : returns the total time as a string

Get the current date:

var days = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"];
var months = ["January", "February", "March", "April", "May", "June",
            "July", "August", "September", "October", "November", "December"];
            
var myDate = new Date();
var fullDate = "Today: " + myDate.getDate() + " " + months[myDate.getMonth()] +
                " " + myDate.getFullYear() + ", " + days[myDate.getDay()];
document.write(fullDate); // Today: 18 August 2015, Tuesday

Arrays are used to convert from numerical values ​​to more familiar names for days of the week and months. Having received the index of the day of the week ( myDate.getDay()) and the index of the month ( myDate.getMonth()), you can get the desired element from the array.

Now we get the current time:

welcome;
var myDate = new Date();
varhour = myDate.getHours();
var minute = myDate.getMinutes();
var second = myDate.getSeconds();
if (minute < 10) {
    minute = "0" + minute;
}
if (second < 10) {
    second = "0" + second;
}
if (hour < 12) {
    welcome = "Good morning";
} else if (hour < 17) {
    welcome = "Good afternoon";
} else {
    welcome = "Good evening";
}
document.write(welcome + ", current time: " + hour +
                ":" + minute + ":" + second); // Good evening, current time is 22:50:39
                

Setting the date and time

In addition to setting the date parameters in the setup constructor, we can also use additional methods of the Date object:

  • setDate() : set the day in a date
  • setMonth() : month setting (counting starts from zero, i.e. month number 0 is January)
  • setFullYear() : sets the year
  • setHours() : set the hour
  • setMinutes() : set minutes
  • setSeconds() : set seconds
  • setMilliseconds() : set milliseconds

Set the date:

var days = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"];
var months = ["January", "February", "March", "April", "May", "June",
            "July", "August", "September", "October", "November", "December"];
            
var myDate = new Date();
myDate.setDate(15);
myDate.setMonth(6);
myDate.setYear(2022);

var fullDate = myDate.getDate() + " " + months[myDate.getMonth()] +
                " " + myDate.getFullYear() + ", " + days[myDate.getDay()];
document.write(fullDate); // 15 July 2022, Monday

When setting values, we can pass in a value greater than the maximum allowed value. For example, set the hour to 54:

myDate.setHour(54);

In this case, the hour value will be 54 – 24 * 2 = 6, and the remaining hours will be two days (24 * 2), which will add two days to the date. The same applies to days, minutes, seconds, milliseconds, and months.