Borislav Hadzhiev
Sat Jan 15 2022ยท3 min read
Photo by Tim Marshall
Use the toLocaleString()
method to convert GMT to local time, e.g.
new Date().toLocaleString()
. When no parameters are passed to the method, it
returns a string formatted according to the visitor's locale and time zone.
// ๐๏ธ can be any valid GMT date/ date+time string const dateStr = 'Sat, 15 Jan 2021 09:24:00 GMT'; const date = new Date(dateStr); // ๐๏ธ "Fri Jan 15 2021 11:24:00 GMT+0200 (Eastern European Standard Time)" console.log(date); // โ get date and time in the visitor's default locale console.log(date.toLocaleString()); // ๐๏ธ "1/15/2021, 11:24:00 AM" // โ Get LOCAL (visitor's time zone) date and time components const hours = date.getHours(); console.log(hours); // ๐๏ธ 11 const minutes = date.getMinutes(); console.log(minutes); // ๐๏ธ 24 const seconds = date.getSeconds(); console.log(seconds); // ๐๏ธ 0
We used the Date.toLocaleString method to convert GMT to locale time.
// ๐๏ธ "1/15/2022, 11:42:10 AM" console.log(new Date().toLocaleString());
The comments above illustrate the formatting if the methods were run with a time zone of Eastern European Standard Time.
The output you see will be different, depending on your default locale and default time zone.
Note that you can use any of the get*
methods to get any of the date and time
components of a date according to the visitor's local time.
const date = new Date(); // ๐๏ธ returns Hour of the date console.log(date.getHours()); // ๐๏ธ 11 // ๐๏ธ returns Minutes of the date console.log(date.getMinutes()); // ๐๏ธ 46 // ๐๏ธ returns Seconds of the date console.log(date.getSeconds()); // ๐๏ธ 6 // ๐๏ธ returns year of the date console.log(date.getFullYear()); // ๐๏ธ 2022 // ๐๏ธ returns month (0-11) // 0 is January, 11 is December console.log(date.getMonth()); // ๐๏ธ 0 // ๐๏ธ returns day of the month (1-31) console.log(date.getDate()); // ๐๏ธ 15
All of the get*
methods return the date or time component according to the
visitor's local date and time.
Note that the getMonth method returns the month of the specified date as a zero-based value (0 = January, 1 = February, etc.)
If you need a complete list of the Date.get*
methods, visit the
MDN docs.
You can use these methods to format the local date and time to the user in many different ways.
Here is an example that formats the local date and time as
YYYY-MM-DD hh:mm:ss
.
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(':') ); } const dateStr = 'Sat, 15 Jan 2021 09:24:00 GMT'; // ๐๏ธ 2022-01-15 14:24:00 (yyyy-mm-dd hh:mm:ss) console.log(formatDate(new Date(dateStr)));
We joined the date components of the date with a hyphen separator and the time components with a colon separator.
/
or tweak this function in any way that suits your use case.Note that, most likely, you shouldn't be storing local date and times in your database.
For consistency, you should mostly use local time when you have to render a date and time to the user, but store the actual values in UTC.
There is a UTC
equivalent for all of the get*
methods we previously covered,
e.g.
getUTCFullYear
vs
getFullYear.
All of the getUTC*
methods return the date or time component according to
universal time.
If you need a complete list of the getUTC*
methods, visit the
MDN docs.
The getUTC*
methods return the date or time component according to universal
time, whereas the get*
methods return them according to local time (the time
zone the visitor's computer is in).
The get*
methods return different results depending on where the user visits
your site from.