Reputation: 179
I want to construct a Date
object along with dynamically selected timezone. I am currently in IST time zone. I want to eliminate the usage of Date.parse()
as it does not behave as expected at times.
let's assume tzOffset
to be +05:30
for now. It could be any other timezone based on what users want. new Date(epochDate).toISOString();
converts the date to UTC timezone. How do I get the date in toISOString()
format but also get it in the desired time zone
const tsConstruct = `${year}-${month}-${date}T${hour}:${min}:00${tzOffset}`;
const epochDate = Date.parse(tsConstruct);
scheduledTs = new Date(epochDate).toISOString();
Upvotes: 0
Views: 308
Reputation: 18292
JavaScript's Date does not store timezone info. It just stores the number of milliseconds from UNIX EPOCH. Then, depending if you use the UTC methods or not, it returns the date and time in UTC or the local time.
You should have to change the date and time, based on the timezone indicated, to UTC or local time, and then store it in a Date object. But, of course, to show the stored time in another timezone different to the local one or UTC, you must do the conversions yourself, so, as @RuChengChong suggested, use a helper library like momentjs
.
Upvotes: 1