Skip to content

Instantly share code, notes, and snippets.

@bigdigital
Created December 16, 2022 08:50
Show Gist options
  • Save bigdigital/b16aba8de826d038696a56964b273340 to your computer and use it in GitHub Desktop.
Save bigdigital/b16aba8de826d038696a56964b273340 to your computer and use it in GitHub Desktop.
zepp get local time
const TIME_SENSOR = hmSensor.createSensor(hmSensor.id.TIME)
let getDateLocal = function () {
let utc = TIME_SENSOR.utc
let d2 = new Date();
let offsetHour = 0
if (TIME_SENSOR.day < d2.getDate() || TIME_SENSOR.hour < d2.getHours()) {
offsetHour = TIME_SENSOR.hour - (d2.getHours() + (d2.getDate() - TIME_SENSOR.day) * 24)
} else if (TIME_SENSOR.day > d2.getDate() || TIME_SENSOR.hour > d2.getHours()) {
offsetHour = (TIME_SENSOR.day - d2.getDate()) * 24 + TIME_SENSOR.hour - d2.getHours()
}
d2.setTime(utc + offsetHour * 60 * 60 * 1000)
return d2
}
let getDateLocalByUTC = function (utc) {
let dateLocal = getDateLocal()
dateLocal.setTime(dateLocal.getTime() + (utc - TIME_SENSOR.utc))
return dateLocal
}
@bigdigital
Copy link
Author

getDateLocal should get the difference between the time sensor and d2 (which represent UTC). It will return a local time based on the difference in hours between sensor and UTC

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment