#new Date() returns UTC time instead of local time
1 messages · Page 1 of 1 (latest)
That's correct. It does always return UTC time, I would imagine for privacy reasons. You could make the time zone configurable in your script and adjust the times manually if you need to display local time.
how do i do that? do i manually add/subtract hours and days or is there an easy way
/**
* Return real time in your country
* @param {*} date time in map `date.now()`
* @param {Number} offset UTC time
* @example TimeZone(date.now(),7)
* @returns
*/
function TimeZone(date, offset) {
const utc = date.getTime() + (date.getTimezoneOffset() * 60000);
const adminTime = new Date(utc + (3600000 * offset));
let hours = adminTime.getHours();
const minutes = adminTime.getMinutes();
const seconds = adminTime.getSeconds();
const amPm = hours >= 12 ? 'PM' : 'AM';
hours = (hours % 12) || 12;
return `${hours}:${minutes}:${seconds} ${amPm}`;
}
ex:
const Time = date.now()
const UTC = 7
TimeZone(Time,UTC)