Published on

How to convert date from any timezone to UTC using Day.js

Authors

If you're working with date expecially with multiple timezone then Day.js, a lightweight Javascript library might be a good choice.

Dependencies

npm i dayjs

Snippet

import dayjs from 'dayjs'
import utc from 'dayjs/plugin/utc';
import timezone from 'dayjs/plugin/timezone';
dayjs.extend(utc);
dayjs.extend(timezone);

const convertToUTC = (date, dateFormat='YYYY-MM-DDTHH:MM:SSZ', tz='Asia/Kolkata')  => {
  return dayjs.tz(date, dateFormat, tz).utc().toDate()
}

Happy parsing date!