X

Js

Date().toLocaleString()

July 15, 2020

/*

Trying to get the time

*/

I was making a project where I had to get the date in my timezone. The problem was my timezone couldn't change, even if the server was in a different zone. At first, I thought this was going to be fast, but it took me a lot longer than I wanted to find the info.

The Date class tells you the current time in milliseconds. If you attach the toLocaleString() method to it, it will give you the time in your current timezone.

/*

toLocaleString() Parameters

*/

toLocaleString() takes two parameters. The first is a string that displays the language you want the time in. Mine is 'en-US'. The second is an options object thats let's you choose how the time is shown(hours, minutes, seconds, timezone). I'm going to be using the default options for the hours, minutes and seconds. This timezone '{timeZone: "America/New_York"}' is the one I'm in.





Language Strings





Timezone Options

About the Author

Christopher Howard

Chris is a Javascript developer with a minor in UI design. He values programming in vanilla code. Fill out the form below to contact him.