Assuming you have the date and time in the user's timezone, and you want to obtain the corresponding UTC day within the range of fromHours and toHours:
Example code:
function getUTCDate(userDate, fromHours, toHours) {
// Convert user date to UTC
const utcDate = new Date(userDate.getTime() + (userDate.getTimezoneOffset() * 60000));
// Get UTC day
let utcDay = utcDate.getUTCDate();
// Adjust UTC day if necessary
if (utcDate.getUTCHours() < fromHours || utcDate.getUTCHours() >= toHours) {
if (utcDate.getUTCHours() < fromHours) {
utcDay--;
} else {
utcDay++;
}
}
// Create and return adjusted UTC date
return new Date(Date.UTC(utcDate.getUTCFullYear(), utcDate.getUTCMonth(), utcDay));
}
Example usage:
// Get UTC date for March 31, 2021, 4:30 PM in Pacific Standard Time (UTC-8) with range from 7 AM to 7 PM
const userDate = new Date('2021-03-31T16:30:00-0800');
const utcDate = getUTCDate(userDate, 7, 19);
console.log(utcDate.toISOString()); // Output: 2021-03-31T23:00:00.000Z
Please start posting anonymously - your entry will be published after you log in or create a new account. This space is reserved only for answers. If you would like to engage in a discussion, please instead post a comment under the question or an answer that you would like to discuss
Asked: 2021-06-09 11:00:00 +0000
Seen: 11 times
Last updated: Oct 15 '22
How can I transform a standard date to a Jalali date in ClickHouse?
What is the process for transforming a date into the desired format using the dayjs module?
Can you assist me with creating a calendar table in POWER BI?
How can I use the If function to exclude non-workdays?
Can the Excel cell entries be transformed into a uniform date and time format?