Skip to content

Instantly share code, notes, and snippets.

@shivam-tripathi
Last active August 10, 2023 09:30
Show Gist options
  • Save shivam-tripathi/aaf71e2a61ce73e37fa5daa48d961348 to your computer and use it in GitHub Desktop.
Save shivam-tripathi/aaf71e2a61ce73e37fa5daa48d961348 to your computer and use it in GitHub Desktop.
Command Line script to convert localtime to UTC. Defaults to IST.
import { parse } from "https://deno.land/std/flags/mod.ts";
const allowed = ['--dt: datetime', '--tz: timezone', '--d: date', '--t: time'];
const flags = parse(Deno.args);
if (Object.keys(flags).length === 1) {
console.log(`TimeZone Date Utility: Help Utility\n\t${allowed.join('\n\t')}`);
console.log('Defaulting to current time and IST TZ.\n');
}
const { dt, tz = '+05:30' } = flags;
const flip = (_tz: string) => _tz.indexOf('+') !== -1 ? _tz.replace('+', '-') : _tz.replace('-', '+');
const currentInTZ = () => new Date(new Date().toISOString().replace('Z', flip(tz))).toISOString().replace('Z', '');
const current = dt ?? currentInTZ();
const [_d, _t] = current.split('T');
const { d = _d, t = _t } = flags;
const raw = `${d}T${t}${tz}`;
console.log(new Date(raw.split(tz)[0]).toString());
console.log(new Date(raw).toUTCString());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment