0

I have the following javascript function, where i am getting 2 variables from our system (expireddate + modifieddate)+ i am getting the current Date, as follow:-

function(){ 

  var expirydate = item.get_item('ExpireDate');             
  var modifieddate = item.get_item('Modified');
  var currentdate = new Date();

now in my case all the variables will be in this format Tue Jun 11 2019 00:00:00 GMT+0100 (British Summer Time).. and i am not facing any issue since my pc is also using the (British Summer Time) date, so the var currentdate = new Date(); will be compatible with other formats... but my question is how i can guarantee that var currentdate = new Date(); will always be in the (British Summer Time) date regardless of the users' PCs settings? so if the user is using another date format, to have the var currentdate = new Date(); in (British Summer Time) date format ? can i for example get the current date from external source?

John John
  • 1
  • 72
  • 238
  • 501
  • Not exactly the same but this may help: https://stackoverflow.com/questions/53707902/working-with-different-timezones-in-javascript – benvc Jun 12 '19 at 23:29
  • 2
    http://momentjs.com/ http://momentjs.com/timezone/ – jmoerdyk Jun 12 '19 at 23:31
  • @jmoerdyk can you advice more on this ? how i can get a consistent Date() using those libraries? – John John Jun 12 '19 at 23:32
  • Use ISO 8601 formatted date / time strings so there's zero ambiguity, eg `2019-06-11T00:00:00+01:00` – Phil Jun 12 '19 at 23:51

1 Answers1

1

I think I understand what you are looking for, and if so, you will want to use toLocaleString(), and tell it what format you would like the result to be in.

The following will result in british time format in the UTC timezone.

let date = new Date()

console.log('UTC:', date.toLocaleString('en-GB', { timeZone: 'UTC' }))
console.log('Guernsey:', date.toLocaleString('en-GB', { timeZone: 'Europe/Guernsey' }))
console.log('Guernsey Time:', date.toLocaleTimeString('en-GB', { timeZone: 'Europe/Guernsey' }))
Get Off My Lawn
  • 34,175
  • 38
  • 176
  • 338
  • ok thanks for the reply. so this will work regardless of the user' PCs regional settings and date/time format such as `dd/mm/yyyy` or `mm/dd/yyyy`? – John John Jun 13 '19 at 00:36
  • It should, I am nowhere near that location, and for me it displays the time of **Guernsey** as `01:49:21` – Get Off My Lawn Jun 13 '19 at 00:50