Remove the Time or Time Zone from a Date in JavaScript

avatar
Borislav Hadzhiev

Last updated: Mar 6, 2024
5 min

banner

# Table of Contents

  1. Remove the Time from a Date using JavaScript
  2. Remove the local Time Zone from a Date using JavaScript

If you need to remove the local Time Zone from a Date, click on the second subheading.

# Remove the Time from a Date using JavaScript

To remove the time from a date, use the getFullYear(), getMonth() and getDate() methods to get the year, month and date of the given date and pass the results to the Date() constructor.

When values for the time components are not provided, they default to 0.

index.js
function removeTime(date = new Date()) { return new Date( date.getFullYear(), date.getMonth(), date.getDate() ); } // ๐Ÿ‘‡๏ธ Tue Jul 25 2023 00:00:00 console.log(removeTime(new Date())); // ๐Ÿ‘‡๏ธ Wed Jan 19 2022 00:00:00 console.log(removeTime(new Date(2022, 0, 19, 9, 30, 0)));

remove time from date

The code for this article is available on GitHub

We created a reusable function that removes the time from a Date object.

The Date() constructor takes the year, monthIndex, day, hours, minutes, seconds and milliseconds as parameters.

We used the following 3 date-related methods in the function:

  • Date.getFullYear() method - returns a four-digit number representing the year that corresponds to a date.

  • Date.getMonth() - returns an integer between 0 (January) and 11 (December) and represents the month for a given date. Yes, unfortunately, the getMonth method is off by 1.

  • Date.getDate() - returns an integer between 1 and 31 representing the day of the month for a specific date.

We didn't provide parameters for the hours, minutes, seconds and milliseconds, so they all got set to 0.

Note that the removeTime function doesn't change the passed in Date object, it returns a new Date object.

Alternatively, you could use the toDateString method.

# Remove the Time from a Date using toDateString

Alternatively, you can use the toDateString() method.

The method returns only the date portion of a Date object, so passing the result to the Date() constructor would remove the time from the date.

index.js
function removeTime(date = new Date()) { return new Date(date.toDateString()); } // ๐Ÿ‘‡๏ธ Tue Jul 25 2023 00:00:00 console.log(removeTime(new Date())); // ๐Ÿ‘‡๏ธ Wed Jan 19 2022 00:00:00 console.log(removeTime(new Date(2022, 0, 19, 9, 30, 0)));

remove time from date using todatestring

The code for this article is available on GitHub

The toDateString method returns a string that contains only the date part of the Date object.

index.js
console.log(new Date().toDateString()); // ๐Ÿ‘‰๏ธ "Tue Jan 18 2022"

There is nothing that relates to the time in the string, so the hours, minutes, seconds and milliseconds get set to 0 in the newly created Date object.

# Remove the local Time Zone from a Date using JavaScript

There are multiple ways to remove the local time zone from a date:

  1. Use the toUTCString() method to convert the date to a string using UTC.
  2. Use the toLocaleDateString() method to format the date according to a specific locale.
  3. Format the date and time consistently, e.g. as YYYY-MM-DD hh:mm:ss.
index.js
const date = new Date(); // โœ… Get UTC string without local time zone // ๐Ÿ‘‡๏ธ Tue, 18 Jan 2022 14:30:00 GMT console.log(date.toUTCString()); // ----------------------------------------------------- // โœ… Get local representation of Date and time (en-US locale) // ๐Ÿ‘‡๏ธ 1/18/2022, 4:30:00 PM console.log(date.toLocaleString('en-US')); // ----------------------------------------------------- // โœ… Get local representation of Date and time (en-GB locale) // ๐Ÿ‘‡๏ธ 18/01/2022, 16:30:00 console.log(date.toLocaleString('en-GB')); // ----------------------------------------------------- // โœ… Format date and time as YYYY-MM-DD hh:mm:ss // (or any other variation by adjusting the code in the function) function padTo2Digits(num) { return num.toString().padStart(2, '0'); } function formatDate(date) { return ( [ date.getFullYear(), padTo2Digits(date.getMonth() + 1), padTo2Digits(date.getDate()), ].join('-') + ' ' + [ padTo2Digits(date.getHours()), padTo2Digits(date.getMinutes()), padTo2Digits(date.getSeconds()), ].join(':') ); } // ๐Ÿ‘‡๏ธ 2022-01-18 16:30:00 (yyyy-mm-dd hh:mm:ss) console.log(formatDate(new Date())); // ๐Ÿ‘‡๏ธ๏ธ 2025-05-04 05:24:07 (yyyy-mm-dd hh:mm:ss) console.log(formatDate(new Date('May 04, 2025 05:24:07')));
The code for this article is available on GitHub

The toUTCString method returns a string representation of the date using the UTC time zone.

index.js
const date = new Date(); // โœ… Get UTC string without local time zone // ๐Ÿ‘‡๏ธ Tue, 18 Jan 2022 14:30:00 GMT console.log(date.toUTCString());

If you need to format the date and time according to a specific locale, use the toLocaleString method.

Here are examples of the en-US and en-GB locales.

index.js
// โœ… Get local representation of Date and time (en-US locale) // ๐Ÿ‘‡๏ธ 01/18/2022, 4:30:00 PM console.log( date.toLocaleString('en-US', { year: 'numeric', month: '2-digit', day: '2-digit', hour: '2-digit', minute: '2-digit', second: '2-digit', }), ); // โœ… Get local representation of Date and time (en-GB locale) // ๐Ÿ‘‡๏ธ 18/01/2022, 16:30:00 console.log( date.toLocaleString('en-GB', { year: 'numeric', month: '2-digit', day: '2-digit', hour: '2-digit', minute: '2-digit', second: '2-digit', }), );

The two parameters we passed to the toLocaleString method are:

  1. locales - a string with a BCP 47 language tag or an array of such strings. You can use any of the available locales, e.g. es-MX for Mexico or en-CA for Canada. If you need more information about this parameter, check out the MDN docs.
  2. options object where we set how the date and time components should be formatted. Read more about the options object in the MDN docs.

We used the options object to set most of the date and time components formatted as 2 digits for consistency.

# Formatting the date yourself

If you need more control over formatting the date and time, write a reusable function.

index.js
// โœ… Format date and time as YYYY-MM-DD hh:mm:ss // (or any other variation by adjusting the code in the function) function padTo2Digits(num) { return num.toString().padStart(2, '0'); } function formatDate(date) { return ( [ date.getFullYear(), padTo2Digits(date.getMonth() + 1), padTo2Digits(date.getDate()), ].join('-') + ' ' + [ padTo2Digits(date.getHours()), padTo2Digits(date.getMinutes()), padTo2Digits(date.getSeconds()), ].join(':') ); } // ๐Ÿ‘‡๏ธ 2022-01-18 16:30:00 (yyyy-mm-dd hh:mm:ss) console.log(formatDate(new Date())); // ๐Ÿ‘‡๏ธ๏ธ 2025-05-04 05:24:07 (yyyy-mm-dd hh:mm:ss) console.log(formatDate(new Date('May 04, 2025 05:24:07')));
The code for this article is available on GitHub

The function makes use of the following 6 Date related methods.

  • Date.getFullYear method - returns a four-digit number representing the year that corresponds to a date.

  • Date.getMonth - returns an integer between 0 (January) and 11 (December) and represents the month for a given date. Yes, unfortunately, the getMonth method is off by 1.

  • Date.getDate - returns an integer between 1 and 31 representing the day of the month for a specific date.

  • Date.getHours - returns the hour for the specified date.

  • Date.getMinutes() - returns the minutes for a date.

  • Date.getSeconds - returns the seconds of a specific date.

The getMonth method returns a zero-based month index from 0 to 11, meaning January is 0 and December is 11.

The getMonth method is zero-based, so we added 1 to its return value.

We placed the year, month and day in an array, so we could join them with a hyphen separator.

index.js
console.log(['2022', '01', '18'].join('-')); // ๐Ÿ‘‰๏ธ '2022-01-18' console.log(['2028', '04', '24'].join('-')); // ๐Ÿ‘‰๏ธ '2026-04-24'

This gets us the date formatted as YYYY-MM-DD.

The next step is to place the return values from the time-related methods in an array and join them with a colon.

index.js
console.log(['05', '24', '36'].join(':')); // ๐Ÿ‘‰๏ธ '05:24:36' console.log(['08', '13', '56'].join(':')); // ๐Ÿ‘‰๏ธ '08:13:56'

We used the addition (+) operator to add a space in the middle of the strings to get the date formatted as YYYY-MM-DD hh:mm:ss.

You can reorder the date components, change the separator to a forward slash /, instead of a hyphen, or make any changes that suit your use case.

I've also written an article on how to create a Date without a Timezone.

# Additional Resources

You can learn more about the related topics by checking out the following tutorials:

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.

Copyright ยฉ 2024 Borislav Hadzhiev