Last updated: Mar 6, 2024
Reading timeยท5 min
If you need to remove the local Time Zone from a Date, click on the second subheading.
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
.
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)));
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
.
removeTime
function doesn't change the passed in Date
object, it returns a new Date
object.Alternatively, you could use the toDateString method.
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.
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)));
The toDateString
method returns a string that contains only the date part of
the Date
object.
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.
There are multiple ways to remove the local time zone from a date:
toUTCString()
method to convert the date to a string using UTC.toLocaleDateString()
method to format the date according to a
specific locale.YYYY-MM-DD hh:mm:ss
.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 toUTCString method returns a string representation of the date using the UTC time zone.
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.
// โ 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:
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.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.
If you need more control over formatting the date and time, write a reusable function.
// โ 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 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.
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.
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.
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.
You can learn more about the related topics by checking out the following tutorials: