How to get the current London time and date using javascript

Viewed 12260

I tried to get the current time and date of London using below code. But it not showing the London time.

var date = new Date();
console.log(date.toLocaleString('en-GB'));

the above one showing as current time and day as per my timezone(India time) 20/12/2018, 10:43:23

Can anyone please help me convert Indian standard time to BST logic.

3 Answers

You can use momentjs as suggested by other answers, if you have lot of date operations(Like date comparison, modification, localisation etc). If you only want to display date as string you can use native javascript date object.

var event = new Date();
console.log(event.toLocaleString('en-GB', { timeZone: 'Europe/London' }));

You have partially done that. All you need to do is pass "timeZone" info to your toLocalseString method.

[Refer Mozilla Docs]https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/toLocaleString

As @Shiva answered, it's correct. But if someone is looking for getting time in hours specifically, here is an example below:

  var rightNow = new Date();
  rightNow = rightNow.toLocaleString('en-GB', {hour: '2-digit',   hour12: false, timeZone: 'Europe/London' });
Related