Everything to know about JavaScript Date

The World Wide Web uses JavaScript, a lightweight programming language. If you want to know when a visitor arrives at your website, how do you use the JavaScript Date object?

The year, month, and day may all be found using the JavaScript date object. Using the JavaScript date object, you may put a countdown timer on your website.

To generate a date object in JavaScript, various Date constructors can be used. Methods to get and set the date and month are provided.

JavaScript Date Constructor:

To build a date object, you can use one of four Date constructors:

  • Date()
  • Date(dateString)
  • Date(miliseconds)
  • Date(Year, month, day, hours, minutes, seconds, miliseconds)

Syntax of JavaScript Date constructors:

  • new Date()
  • new Date(value)
  • new Date(dateString)
  • new Date(dateObject)
  • new Date(year, monthIndex)
  • new Date(year, monthIndex, day)
  • new Date(year, monthIndex, day, hours)
  • new Date(year, monthIndex, day, hours, minutes)
  • new Date(year, monthIndex, day, hours, minutes, seconds)
  • new Date(year, monthIndex, day, hours, minutes, seconds, milliseconds)

How JavaScript Date Return value work?

The Date() constructor returns a Date object when the new Date() is called. It returns a Date object when the toString() function delivers the literal string and returns Invalid Date if invoked with an invalid date string.

When the Date() method is called without the new keyword, it produces the same string representation of the current time and date as the new Date (). This is what function toString() does. Any inputs passed to the Date() function (without the new keyword) are omitted; it always produces a string representation of the current time and date, regardless of whether it’s called with an inaccurate date string — or even with any random object or basic as an argument.

JavaScript Date Methods you need to learn:

Let’s have a look at the JavaScript date methods and their descriptions.

1.      getDate()

It returns an integer value between 1 and 31 that reflects the day for the provided date, according to local time.

2.     getDay()

According to the local time, it returns an integer value between 0 and 6 that reflects the day of the week.

3.      getFullYears()

It gives you an integer value that indicates the year in local time.

4.      getHours()

It returns an integer value ranging from 0 to 23 that indicates the hours in local time.

5.      getMilliseconds()

It gives you an integer value between 0 and 999 that indicates milliseconds in local time.

6.      getMinutes()

It produces an integer value between 0 and 59, according to the minutes in local time.

7.      getMonth()

On the basis of local time, it generates an integer value between 0 and 11 that indicates the month.

8.      getSeconds()

It produces an integer value between 0 and 60, indicating the number of seconds in local time.

9.      getUTCDate()

On the basis of universal time, it produces an integer value between 1 and 31 that indicates the day for the provided date.

10. getUTCDay()

On the basis of universal time, it generates an integer value between 0 and 6 that indicates the day of the week.

11. getUTCFullYears()

According to universal time, it returns an integer value that indicates the year.

12. getUTCHours()

An integer between 0 and 23 is returned, according to the number of hours according to universal time.

13. getUTCMinutes()

When using universal time, it returns an integer value between 0 and 59, which reflects the number of minutes.

14. getUTCMonth()

It Returns the month’s universal time-based integer value, which ranges from 0 to 11.

15. getUTCSeconds()

An integer between 0 and 60 is returned to represent the number of seconds in a second using universal time.

16. setDate()

setDate() is a function that uses the local time to determine the day of the week for a given date.

17. setDay()

setDay() is a function that determines the day of the week based on the current local time.

18. setFullYears()

This function sets the year to the given date in local time using the setFullYears() parameter.

19. setHours()

It uses the local time to determine the hour value for a given date.

20. setMilliseconds()

It uses the local time to determine the millisecond value for a specific date.

21. setMinutes()

It uses the local time to determine the minute value for a given date.

22. setMonth()

setMonth() uses the local time to determine the month for a given date.

23. setSeconds()

setSeconds() uses the local time to add a second value to the provided date.

24. setUTCDate()

It uses the universal time to determine the day of the week for the given date.

25. setUTCDay()

It uses the universal time to determine which day of the week it is.

26. setUTCFullYears()

It uses the universal time to determine the year for the supplied date.

27. setUTCHours()

It uses the universal time to set the hour value for the provided date.

28. setUTCMilliseconds()

It uses the universal time to set the millisecond value for the given date.

29. setUTCMinutes()

On the basis of universal time, it changes the given date’s minute value to a value of 0.

30. setUTCMonth()

It uses the universal time to determine the month for a given date.

31. setUTCSeconds()

It adds a second value based on universal time to the specified date.

32. toDateString()

It returns a Date object from a string.

33. toISOString()

The date is returned as an ISO formatted string in response to the query.

34. toJSON()

It provides a string representation of the Date object as a result of using this method. During JSON serialization, it also set the Date object.

35. toString()

The date is returned as a string.

36. toTimeString()

A Date object’s time component is returned by this method.

37. toUTCString()

It uses the UTC time zone to convert the provided date to a string.

38. valueOf()

A Date object’s primitive value is returned by this method.

Examples of javascript date functions:

Example#1:

Let’s take a look at a simple date object printing example. The time and date are printed together.

Current Date and Time: <span id="txt"></span>  
<script>  
var today=new Date();  
document.getElementById('txt').innerHTML=today;  
</script>  

Output:

Current Date and Time: Tue Nov 02 2021 13:17:42 GMT-0700 (Pacific Daylight Time)

Example#2:

Let’s have a look at another code to print date/month/year.

<script>  
var date=new Date();  
var day=date.getDate();  
var month=date.getMonth()+1;  
var year=date.getFullYear();  
document.write("<br>Date is: "+day+"/"+month+"/"+year);
</script>

Output:

Date is: 2/11/2021

Example#3:

Let’s have a look at an easy example to print the current time of system

Current Time: <span id="txt"></span>  
<script>  
var today=new Date();  
var h=today.getHours();  
var m=today.getMinutes();  
var s=today.getSeconds();  
document.getElementById('txt').innerHTML=h+":"+m+":"+s;  
</script>  

Output:

Current Time: 13:17:42

Conclusion:

Now that you know how to get the current date and time in JavaScript, you should be able to write codes. Adding this script to an operation and sending it to a log file will create a timestamp for you. Keep connected to learn more programming tricks!