0

am building a livescore but the API am using is returning time that is 2hrs ahead of my local time, and I can't convert it to my country's time since different users from different countries will be accessing the livescore. I don't know the api timezone.. But I want a function that will get the api timezone and convert to the user's local time

Sebastian D'Agostino
  • 1,575
  • 2
  • 27
  • 44
idevosm
  • 45
  • 4
  • 1
    Possible duplicate of [Convert date to another timezone in JavaScript](https://stackoverflow.com/questions/10087819/convert-date-to-another-timezone-in-javascript) – Sebastian D'Agostino Jun 16 '18 at 15:23

2 Answers2

0

For http://momentjs.com users, you can now use 

http://momentjs.com/timezone/docs/.

Using it, your function would look something like this:

function toTimeZone(time, zone) {
    var format = 'YYYY/MM/DD/HH:mm:ss ZZ'; 
    return moment(time, format).tz(zone).format(format); 
 }
hashed_name
  • 553
  • 6
  • 21
0

You can use momentjs . Example fiddle `

var serverdate = "Fri Jun 15 2018 08:03:23 GMT-0700 (Pacific Daylight Time)"

var date3 =  moment(serverdate) // this will automatically convert the date to the time zone on  user's machine

alert(date3)
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.22.2/moment.js"></script>

`

shmit
  • 2,306
  • 2
  • 16
  • 20