Mathew
Mathew

Reputation: 85

Display day/date and time from specific timezone using javascript for html

I have a project that requires the day, date, and time to be displayed on a website from a specific timezone, Central European Time. Ideally I'd like to display it in this format:

Tue 4 May 05:52:33 CET

I've been able to get the time to display on websites before, but this is usually getting the time from the user's end. In this case it should display my time.

I know there are quite a few topics about this already, but I really haven't been able to get it to work, plus including the day and date is an additional difficulty.

Any help would be amazing :-)

Upvotes: 0

Views: 1649

Answers (1)

Ashwin Balamohan
Ashwin Balamohan

Reputation: 3332

Use the moment.js library.

Let's say your timezone is "America/Los_Angeles":

var myTimezone = "America/Los_Angeles";
var datetimeFormat = "ddd DD MMM hh:mm:ss z";
var timeString = moment().tz(myTimezone).format(datetimeFormat);
console.log(timeString);

This will format the user's time into your timezone. For more information, see: http://momentjs.com/timezone/

Upvotes: 1

Related Questions