As the title stated, I want to get the start time and end time of a day for certain timezone and convert them to utc time. Here is my part of my implementation:
//convert current local time to specified timezone time var converted = moment.tz(moment(), timezone).format("YYYY-MM-DD"); var full_format = "YYYY-MM-DD HH:mm:ss"; // get start time and end time in the timezone var start = converted + " 00:00:00"; var end = converted + " 23:59:59"; // how to convert them in utc time by momentjs or other methods? var utc_start_time = ? var utc_end_time = ?
The question is how to convert the time in certain timezone to utc time. Or is there any other decent solutions to it? Thank you!
Edit:
I figured out a way to make it myself which is not quite decent.
var converted = moment.tz(moment(), timezone).format("YYYY-MM-DD"); var full_format = "YYYY-MM-DD HH:mm:ss"; var start = converted + " 00:00:00"; var end = converted + " 23:59:59"; var utc_start_time = moment(start).add(utc_offset * -1, 'hours').format(full_format); var utc_end_time = moment(end).add(utc_offset * -1, 'hours').format(full_format);
Any suggestions on improvements are welcome. Thanks
Advertisement
Answer
Depending on what exactly you want to do:
// for the current day var start = moment.tz(timezone).startOf('day').utc(); var end = moment.tz(timezone).endOf('day').utc(); // for a specific moment (m) var start = m.clone().tz(timezone).startOf('day').utc(); var end = m.clone().tz(timezone).endOf('day').utc(); // for a specific calendar date var m = moment.tz(date, format, timezone); var start = m.clone().startOf('day').utc(); var end = m.clone().endOf('day').utc();
You can then format start
and end
however you like with the format
function.
Keep in mind also that not every day starts at midnight in every time zone, nor do all local days have 24 hours.