A Journey through JavaScript Date Functions: Everything You Need to Know
Tags: javascript, datefunctions, codingtips, beginners
Published: 2023-06-29 16:43:59



## Introduction:
Working with dates is a common task in JavaScript programming. JavaScript provides a rich set of built-in Date functions that allow developers to manipulate and format dates with ease. In this comprehensive guide, we will explore various Date functions in JavaScript, providing detailed explanations and examples for each function. By the end, you'll have a solid understanding of how to work with dates effectively in JavaScript.

**List of JavaScript Date Functions:**
1. Date()
2. new Date()
3. getDate()
4. getDay()
5. getFullYear()
6. getHours()
7. getMilliseconds()
8. getMinutes()
9. getMonth()
10. getSeconds()
11. getTime()
12. getTimezoneOffset()
13. getUTCDate()
14. getUTCDay()
15. getUTCFullYear()
16. getUTCHours()
17. getUTCMilliseconds()
18. getUTCMinutes()
19. getUTCMonth()
20. getUTCSeconds()
21. setDate()
22. setFullYear()
23. setHours()
24. setMilliseconds()
25. setMinutes()
26. setMonth()
27. setSeconds()
28. setTime()
29. setUTCDate()
30. setUTCFullYear()
31. setUTCHours()
32. setUTCMilliseconds()
33. setUTCMinutes()
34. setUTCMonth()
35. setUTCSeconds()
36. toDateString()
37. toISOString()
38. toJSON()
39. toLocaleDateString()
40. toLocaleString()
41. toLocaleTimeString()
42. toString()
43. toTimeString()
44. toUTCString()
45. UTC()

Now, let's delve into each function, providing a detailed explanation and example for better understanding.

**1. Date():**
The `Date()` function is used to create a new Date object representing the current date and time. Here's an example:

```javascript
const currentDate = Date();
console.log(currentDate);
```
Output:
```
Tue Jun 29 2023 12:30:00 GMT+0530 (India Standard Time)
```
Explanation:
The `Date()` function, when called without any arguments, returns a string representing the current date and time. The output format varies based on the browser's implementation.

**2. new Date():**
The `new Date()` constructor creates a new Date object based on the provided arguments. Here's an example:

```javascript
const specificDate = new Date('2023-06-29');
console.log(specificDate);
```
Output:
```
Tue Jun 29 2023 00:00:00 GMT+0530 (India Standard Time)
```
Explanation:
By passing a specific date string as an argument to `new Date()`, we can create a Date object representing that particular date. The output displays the date and time in the local time zone.

**3. getDate():**
The `getDate()` method returns the day of the month (from 1 to 31) for the specified Date object. Here's an example:

```javascript
const date = new Date();
const dayOfMonth = date.getDate();
console.log(dayOfMonth);
```
Output:
```
29
```
Explanation:
The `getDate()` method retrieves the day of the month from the current Date object. In this example, it returns the value "29" since the current date is the 29th.

**4. getDay():**
The `getDay()` method returns the day of the week (from 0 to 6) for the specified Date object. Here's an example:

```javascript
const date = new Date();
const dayOfWeek = date.getDay();
console.log(dayOfWeek);
```
Output:
```
2
```
Explanation:
The `getDay()` method retrieves the day of the week from the current Date object. It returns a value from 0 to 6, where 0 represents Sunday, 1 represents Monday, and so on. In this example, the output is "2" since Tuesday is the second day of the week.

**5. getFullYear():**
The `getFullYear()` method returns the four-digit year for the specified Date object. Here's an example:

```javascript
const date = new Date();
const year = date.getFullYear();
console.log(year);
```
Output:
```
2023
```
Explanation:
The `getFullYear()` method retrieves the four-digit year from the current Date object. In this example, it returns the current year, which is "2023".

**6. getHours():**
The `getHours()` method returns the hour (from 0 to 23) for the specified Date object. Here's an example:

```javascript
const date = new Date();
const hours = date.getHours();
console.log(hours);
```
Output:
```
12
```
Explanation:
The `getHours()` method retrieves the hour from the current Date object in 24-hour format. In this example, it returns the current hour, which is "12" (12 PM).

**7. getMilliseconds():**
The `getMilliseconds()` method returns the milliseconds (from 0 to 999) for the specified Date object. Here's an example:

```javascript
const date = new Date();
const milliseconds = date.getMilliseconds();
console.log(milliseconds);
```
Output:
```
500
```
Explanation:
The `getMilliseconds()` method retrieves the milliseconds from the current Date object. In this example, it returns "500".

**8. getMinutes():**
The `getMinutes()` method returns the minutes (from 0 to 59) for the specified Date object. Here's an example:

```javascript
const date = new Date();
const minutes = date.getMinutes();
console.log(minutes);
```
Output:
```
30
```
Explanation:
The `getMinutes()` method retrieves the minutes from the current Date object. In this example, it returns the current minutes, which is "30".

**9. getMonth():**
The `getMonth()` method returns the month (from 0 to 11) for the specified Date object. Here's an example:

```javascript
const date = new Date();
const month = date.getMonth();
console.log(month);
```
Output:
```
5
```
Explanation:
The `getMonth()` method retrieves the month from the current Date object. It returns a value from 0 to 11, where 0 represents January, 1 represents February, and so on. In this example, the output is "5" since June is the sixth month.

**10. getSeconds():**
The `getSeconds()` method returns the seconds (from 0 to 59) for the specified Date object. Here's an example:

```javascript
const date = new Date();
const seconds = date.getSeconds();
console.log(seconds);
```
Output:
```
0
```
Explanation:
The `getSeconds()` method retrieves the seconds from the current Date object. In this example, it returns the current seconds, which is "0".

**11. getTime():**
The `getTime()` method returns the numeric value corresponding to the time for the specified Date object. Here's an example:

```javascript
const date = new Date();
const time = date.getTime();
console.log(time);
```
Output:
```
1677725888111
```
Explanation:
The `getTime()` method retrieves the time in milliseconds from the Unix epoch (January 1, 1970, 00:00:00 UTC) to

 the current Date object. In this example, it returns the current time in milliseconds.

**12. getTimezoneOffset():**
The `getTimezoneOffset()` method returns the time zone offset in minutes for the current locale. Here's an example:

```javascript
const date = new Date();
const timezoneOffset = date.getTimezoneOffset();
console.log(timezoneOffset);
```
Output:
```
-330
```
Explanation:
The `getTimezoneOffset()` method retrieves the time zone offset in minutes for the current locale. It returns a positive value if the local time zone is behind UTC and a negative value if it is ahead. In this example, the output is "-330" since the local time zone is UTC+05:30.

**13. getUTCDate():**
The `getUTCDate()` method returns the day of the month (from 1 to 31) for the specified Date object, according to universal time. Here's an example:

```javascript
const date = new Date();
const utcDayOfMonth = date.getUTCDate();
console.log(utcDayOfMonth);
```
Output:
```
29
```
Explanation:
The `getUTCDate()` method retrieves the day of the month from the current Date object, according to universal time (UTC). In this example, it returns the value "29" since the current date in UTC is the 29th.

**14. getUTCDay():**
The `getUTCDay()` method returns the day of the week (from 0 to 6) for the specified Date object, according to universal time. Here's an example:

```javascript
const date = new Date();
const utcDayOfWeek = date.getUTCDay();
console.log(utcDayOfWeek);
```
Output:
```
2
```
Explanation:
The `getUTCDay()` method retrieves the day of the week from the current Date object, according to universal time (UTC). It returns a value from 0 to 6, where 0 represents Sunday, 1 represents Monday, and so on. In this example, the output is "2" since Tuesday is the second day of the week in UTC.

**15. getUTCFullYear():**
The `getUTCFullYear()` method returns the four-digit year for the specified Date object, according to universal time. Here's an example:

```javascript
const date = new Date();
const utcYear = date.getUTCFullYear();
console.log(utcYear);
```
Output:
```
2023
```
Explanation:
The `getUTCFullYear()` method retrieves the four-digit year from the current Date object, according to universal time (UTC). In this example, it returns the current year, which is "2023".

**16. getUTCHours():**
The `getUTCHours()` method returns the hour (from 0 to 23) for the specified Date object, according to universal time. Here's an example:

```javascript
const date = new Date();
const utcHours = date.getUTCHours();
console.log(utcHours);
```
Output:
```
7
```
Explanation:
The `getUTCHours()` method retrieves the hour from the current Date object, according to universal time (UTC), in 24-hour format. In this example, it returns the current hour, which is "7" (7 AM) in UTC.

**17. getUTCMilliseconds():**
The `getUTCMilliseconds()` method returns the milliseconds (from 0 to 999) for the specified Date object, according to universal time. Here's an example:

```javascript
const date = new Date();
const utcMilliseconds = date.getUTCMilliseconds();
console.log(utcMilliseconds);
```
Output:
```
500
```


Explanation:
The `getUTCMilliseconds()` method retrieves the milliseconds from the current Date object, according to universal time (UTC). In this example, it returns "500".

**18. getUTCMinutes():**
The `getUTCMinutes()` method returns the minutes (from 0 to 59) for the specified Date object, according to universal time. Here's an example:

```javascript
const date = new Date();
const utcMinutes = date.getUTCMinutes();
console.log(utcMinutes);
```
Output:
```
0
```
Explanation:
The `getUTCMinutes()` method retrieves the minutes from the current Date object, according to universal time (UTC). In this example, it returns the current minutes, which is "0" in UTC.

**19. getUTCMonth():**
The `getUTCMonth()` method returns the month (from 0 to 11) for the specified Date object, according to universal time. Here's an example:

```javascript
const date = new Date();
const utcMonth = date.getUTCMonth();
console.log(utcMonth);
```
Output:
```
5
```
Explanation:
The `getUTCMonth()` method retrieves the month from the current Date object, according to universal time (UTC). It returns a value from 0 to 11, where 0 represents January, 1 represents February, and so on. In this example, the output is "5" since June is the sixth month in UTC.

**20. getUTCSeconds():**
The `getUTCSeconds()` method returns the seconds (from 0 to 59) for the specified Date object, according to universal time. Here's an example:

```javascript
const date = new Date();
const utcSeconds = date.getUTCSeconds();
console.log(utcSeconds);
```
Output:
```
0
```
Explanation:
The `getUTCSeconds()` method retrieves the seconds from the current Date object, according to universal time (UTC). In this example, it returns the current seconds, which is "0" in UTC.

**21. setDate():**
The `setDate()` method sets the day of the month (from 1 to 31) for the specified Date object. Here's an example:

```javascript
const date = new Date();
date.setDate(15);
console.log(date);
```
Output:
```
Thu Jun 15 2023 12:30:00 GMT+0530 (India Standard Time)
```
Explanation:
The `setDate()` method sets the day of the month for the current Date object. In this example, it sets the day to the 15th, resulting in a new Date object with the updated day.

**22. setFullYear():**
The `setFullYear()` method sets the four-digit year for the specified Date object. Here's an example:

```javascript
const date = new Date();
date.setFullYear(2022);
console.log(date);
```
Output:
```
Thu Jun 29 2022 12:30:00 GMT+0530 (India Standard Time)
```
Explanation:
The `setFullYear()` method sets the year for the current Date object. In this example, it sets the year to 2022, resulting in a new Date object with the updated year.

**23. setHours():**
The `setHours()` method sets the hour (from 0 to 23) for the specified Date object. Here's an example:

```javascript
const date = new Date();
date.setHours(18);
console.log(date);
```
Output:
```
Tue Jun 29 2023 18:30:00 GMT+0530 (India Standard Time)
```
Explanation:
The `setHours()` method sets the hour

 for the current Date object. In this example, it sets the hour to 18 (6 PM), resulting in a new Date object with the updated hour.

**24. setMilliseconds():**
The `setMilliseconds()` method sets the milliseconds (from 0 to 999) for the specified Date object. Here's an example:

```javascript
const date = new Date();
date.setMilliseconds(500);
console.log(date);
```
Output:
```
Tue Jun 29 2023 12:30:00 GMT+0530 (India Standard Time)
```
Explanation:
The `setMilliseconds()` method sets the milliseconds for the current Date object. In this example, it sets the milliseconds to 500, resulting in a new Date object with the updated milliseconds.

**25. setMinutes():**
The `setMinutes()` method sets the minutes (from 0 to 59) for the specified Date object. Here's an example:

```javascript
const date = new Date();
date.setMinutes(45);
console.log(date);
```
Output:
```
Tue Jun 29 2023 12:45:00 GMT+0530 (India Standard Time)
```
Explanation:
The `setMinutes()` method sets the minutes for the current Date object. In this example, it sets the minutes to 45, resulting in a new Date object with the updated minutes.

**26. setMonth():**
The `setMonth()` method sets the month (from 0 to 11) for the specified Date object. Here's an example:

```javascript
const date = new Date();
date.setMonth(8);
console.log(date);
```
Output:
```
Sun Sep 29 2023 12:30:00 GMT+0530 (India Standard Time)
```
Explanation:
The `setMonth()` method sets the month for the current Date object. In this example, it sets the month to September (8th month), resulting in a new Date object with the updated month.

**27. setSeconds():**
The `setSeconds()` method sets the seconds (from 0 to 59) for the specified Date object. Here's an example:

```javascript
const date = new Date();
date.setSeconds(15);
console.log(date);
```
Output:
```
Tue Jun 29 2023 12:30:15 GMT+0530 (India Standard Time)
```
Explanation:
The `setSeconds()` method sets the seconds for the current Date object. In this example, it sets the seconds to 15, resulting in a new Date object with the updated seconds.

**28. setTime():**
The `setTime()` method sets the numeric value corresponding to the time for the specified Date object. Here's an example:

```javascript
const date = new Date();
date.setTime(1625040000000);
console.log(date);
```
Output:
```
Thu Jun 29 2021 00:00:00 GMT+0530 (India Standard Time)
```
Explanation:
The `setTime()` method sets the time in milliseconds for the current Date object. In this example, it sets the time to the Unix timestamp value corresponding to June 30, 2021, resulting in a new Date object with the updated time.

**29. setUTCDate():**
The `setUTCDate()` method sets the day of the month (from 1 to 31) for the specified Date object, according to universal time. Here's an example:

```javascript
const date = new Date();
date.setUTCDate(15);
console.log(date);
```
Output:
```
Thu Jun 15 2023 12:30:00 GMT+0530 (India Standard Time)
```
Explanation:
The `setUTC

Date()` method sets the day of the month for the current Date object, according to universal time (UTC). In this example, it sets the day to the 15th, resulting in a new Date object with the updated day in UTC.

**30. setUTCFullYear():**
The `setUTCFullYear()` method sets the four-digit year for the specified Date object, according to universal time. Here's an example:

```javascript
const date = new Date();
date.setUTCFullYear(2022);
console.log(date);
```
Output:
```
Thu Jun 29 2022 12:30:00 GMT+0530 (India Standard Time)
```
Explanation:
The `setUTCFullYear()` method sets the year for the current Date object, according to universal time (UTC). In this example, it sets the year to 2022, resulting in a new Date object with the updated year in UTC.

**31. setUTCHours():**
The `setUTCHours()` method sets the hour (from 0 to 23) for the specified Date object, according to universal time. Here's an example:

```javascript
const date = new Date();
date.setUTCHours(18);
console.log(date);
```
Output:
```
Tue Jun 29 2023 18:30:00 GMT+0530 (India Standard Time)
```
Explanation:
The `setUTCHours()` method sets the hour for the current Date object, according to universal time (UTC), in 24-hour format. In this example, it sets the hour to 18 (6 PM) in UTC, resulting in a new Date object with the updated hour.

**32. setUTCMilliseconds():**
The `setUTCMilliseconds()` method sets the milliseconds (from 0 to 999) for the specified Date object, according to universal time. Here's an example:

```javascript
const date = new Date();
date.setUTCMilliseconds(500);
console.log(date);
```
Output:
```
Tue Jun 29 2023 12:30:00 GMT+0530 (India Standard Time)
```
Explanation:
The `setUTCMilliseconds()` method sets the milliseconds for the current Date object, according to universal time (UTC). In this example, it sets the milliseconds to 500, resulting in a new Date object with the updated milliseconds in UTC.

**33. setUTCMinutes():**
The `setUTCMinutes()` method sets the minutes (from 0 to 59) for the specified Date object, according to universal time. Here's an example:

```javascript
const date = new Date();
date.setUTCMinutes(45);
console.log(date);
```
Output:
```
Tue Jun 29 2023 12:45:00 GMT+0530 (India Standard Time)
```
Explanation:
The `setUTCMinutes()` method sets the minutes for the current Date object, according to universal time (UTC). In this example, it sets the minutes to 45 in UTC, resulting in a new Date object with the updated minutes.

**34. setUTCMonth():**
The `setUTCMonth()` method sets the month (from 0 to 11) for the specified Date object, according to universal time. Here's an example:

```javascript
const date = new Date();
date.setUTCMonth(8);
console.log(date);
```
Output:
```
Sun Sep 29 2023 12:30:00 GMT+0530 (India Standard Time)
```
Explanation:
The `setUTCMonth()` method sets the month for the current Date object, according to universal time (UTC). In this example, it sets the month to September (8th month) in

 UTC, resulting in a new Date object with the updated month.

**35. setUTCSeconds():**
The `setUTCSeconds()` method sets the seconds (from 0 to 59) for the specified Date object, according to universal time. Here's an example:

```javascript
const date = new Date();
date.setUTCSeconds(15);
console.log(date);
```
Output:
```
Tue Jun 29 2023 12:30:15 GMT+0530 (India Standard Time)
```
Explanation:
The `setUTCSeconds()` method sets the seconds for the current Date object, according to universal time (UTC). In this example, it sets the seconds to 15 in UTC, resulting in a new Date object with the updated seconds.

**36. toDateString():**
The `toDateString()` method converts the date portion of a Date object into a human-readable string representation. Here's an example:

```javascript
const date = new Date();
const dateString = date.toDateString();
console.log(dateString);
```
Output:
```
"Tue Jun 29 2023"
```
Explanation:
The `toDateString()` method converts the date portion of the Date object into a string representation in the format "Day Month Date Year". In this example, it returns a string representing the date.

**37. toISOString():**
The `toISOString()` method returns a string representation of the Date object in ISO 8601 format. Here's an example:

```javascript
const date = new Date();
const isoString = date.toISOString();
console.log(isoString);
```
Output:
```
"2023-06-29T07:00:00.000Z"
```
Explanation:
The `toISOString()` method converts the Date object into a string representation in the ISO 8601 format. The returned string represents the date, time, and timezone offset. In this example, it returns the ISO string representation of the current date.

**38. toJSON():**
The `toJSON()` method returns a string representation of the Date object in JSON format. Here's an example:

```javascript
const date = new Date();
const jsonString = date.toJSON();
console.log(jsonString);
```
Output:
```
"2023-06-29T12:30:00.000Z"
```
Explanation:
The `toJSON()` method converts the Date object into a string representation in JSON format. The returned string represents the date, time, and timezone offset in ISO 8601 format. In this example, it returns the JSON string representation of the current date.

**39. toLocaleDateString():**
The `toLocaleDateString()` method converts the date portion of the Date object into a human-readable string representation based on the current locale. Here's an example:

```javascript
const date = new Date();
const localeDateString = date.toLocaleDateString();
console.log(localeDateString);
```
Output:
```
"6/29/2023"
```
Explanation:
The `toLocaleDateString()` method converts the date portion of the Date object into a string representation based on the current locale's formatting conventions. The specific format may vary based on the user's locale. In this example, it returns a localized string representing the date.

**40. toLocaleString():**
The `toLocaleString()` method converts the Date object into a human-readable string representation based on the current locale. Here's an example:

```javascript
const date = new Date();
const localeString = date.toLocaleString();
console.log(localeString);
```
Output:
```
"6/29/2023, 12:30:00 PM"
```
Explanation:
The `toLocaleString()` method converts the Date object into a string representation based on the current locale's formatting conventions. The specific format may vary based on the user's locale. In this example, it returns a localized string representing the date and time.

**41. toLocaleTimeString():**
The `toLocaleTimeString()` method converts the time portion of the Date object into a human-readable string representation based on the current locale. Here's an example:

```javascript
const date = new Date();
const localeTimeString = date.toLocaleTimeString();
console.log(localeTimeString);
```
Output:
```
"12:30:00 PM"
```
Explanation:
The `toLocaleTimeString()` method converts the time portion of the Date object into a string representation based on the current locale's formatting conventions. The specific format

 may vary based on the user's locale. In this example, it returns a localized string representing the time.

**42. toString():**
The `toString()` method converts the Date object into a string representation. Here's an example:

```javascript
const date = new Date();
const string = date.toString();
console.log(string);
```
Output:
```
"Tue Jun 29 2023 12:30:00 GMT+0530 (India Standard Time)"
```
Explanation:
The `toString()` method converts the Date object into a string representation using a default format. In this example, it returns a string representing the date, time, and timezone offset.

**43. toTimeString():**
The `toTimeString()` method converts the time portion of the Date object into a string representation. Here's an example:

```javascript
const date = new Date();
const timeString = date.toTimeString();
console.log(timeString);
```
Output:
```
"12:30:00 GMT+0530 (India Standard Time)"
```
Explanation:
The `toTimeString()` method converts the time portion of the Date object into a string representation. In this example, it returns a string representing the time and timezone offset.

**44. toUTCString():**
The `toUTCString()` method converts the Date object into a string representation in UTC format. Here's an example:

```javascript
const date = new Date();
const utcString = date.toUTCString();
console.log(utcString);
```
Output:
```
"Tue, 29 Jun 2023 07:00:00 GMT"
```
Explanation:
The `toUTCString()` method converts the Date object into a string representation in UTC format. In this example, it returns a string representing the date, time, and UTC timezone.

**45. valueOf():**
The `valueOf()` method returns the numeric value corresponding to the time for the specified Date object. Here's an example:

```javascript
const date = new Date();
const value = date.valueOf();
console.log(value);
```
Output:
```
1687349400000
```
Explanation:
The `valueOf()` method returns the time value of the Date object as the number of milliseconds since January 1, 1970, 00:00:00 UTC. In this example, it returns the numeric value representing the current date and time in milliseconds.

These are some of the commonly used date functions in JavaScript. They allow you to manipulate, format, and convert date objects based on your requirements. Understanding and utilizing these functions will help you work effectively with dates and times in JavaScript.