Cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

My countdown app is using UTC. How can I use timezone of the user?

I have my app for counting down to Christmas nearly complete but it seams the time it's using to calculate countdown is 8 hours ahead of where I'm at which is PST. How do I get it to use the users correct time for calculation. By the way, I'm a complete noob to this so bear with me, Thank you. Below is the code I'm using. 

import clock from "clock";
import document from "document";
import { preferences } from "user-settings";
import * as util from "../common/utils";

// Update the clock every second
clock.granularity = "seconds";

// Get a handle on the <text> element
const myLabel = document.getElementById("myLabel");

//This is the target
let end = new Date("2018-12-25");

// Update the <text> element every tick with the current time
clock.ontick = (evt) => {
  //Now Date/time
  let today = evt.date;
  
  //Difference in miliseconds
  let diff = Math.abs(end - today);
  
  let days = parseInt(diff / 86400000);
  diff -= days * 86400000
  
  let hours = parseInt(diff / 3600000)
  diff -= hours * 3600000
  
  let mins = parseInt(diff / 60000)
  diff -= mins * 60000
  
  let secs = parseInt(diff / 1000)
  diff -= secs * 1000
   
  console.log()
  myLabel.text = util.zeroPad(days) + ":" + util.zeroPad(hours) + ":" + util.zeroPad(mins) + ":" + util.zeroPad(secs);
}
Ionic, retired Blaze.
Best Answer
0 Votes
4 REPLIES 4

Odd.  The Date() function on your device is retunring time in UTC.  Fitbit STILL does not include the timezone in it's Date() function (I hate them for this), so you have to poll the current timezone from your phone (or device you sync with). 

 

If you just want it for California for now until Christmas, simply subtract 8 hours from the GMT time you are using. 

 

let end = new Date("2018-12-25 -0800"):

should do it.  

If you want it for any timezone, you can poll for the current timezone from your phone (a pain) and adjust from there.

 

I hope this helps.

Best Answer

@FlyFrosty

Thanks for the help.

I tried your suggestion but it returns Nah:Nah:Nah:Nah

Ionic, retired Blaze.
Best Answer
0 Votes

Ha!  I think Fitbit just face-palmed me.

 

There are 3,600,000 miliseconds in an hour.  You could multiply this value by your timezone offset and add/subtract from the Christmas date.

 

end = end + (3600000 * -8);

Best Answer

@FlyFrosty

Thanks. I ended up just adding 8 to the hours output:

 myLabel.text = util.zeroPad(days) + ":" + util.zeroPad(hours+8) + ":" + util.zeroPad(mins) + ":" + util.zeroPad(secs);

That fixes it for my timezone temporarily, would really like to have it be correct for anybody that uses it though. It would also be cool to have it display "Merry Christmas" when it reaches 00:00:00:00 too but I'm still very new to this. Thanks again for your suggestions.

 

Ionic, retired Blaze.
Best Answer