DEV Community

Cover image for How I Make a Timezone Manipulation Time.ts in Deno
Burhanuddin Ahmed
Burhanuddin Ahmed

Posted on • Updated on

How I Make a Timezone Manipulation Time.ts in Deno

Deno just launched last May 2020, but the hype was already felt among JS / TS developers even before its launch. Hundreds of libraries / modules have immediately appeared since the first day Deno was released. Now I have seen more than ten Deno web frameworks that have been created by the community. The most popular one is Oak Server but I didn't want to be mainstream like others, I choose to use Attain web middleware framework, also I created a web project started for the Attain framework, Denamo.

I will not discuss about Denamo here, maybe it will be discussed later.

Suddenly I was thinking to make a module for datetime, yes even though there was a JS moment in Deno. But it seems that timezone manipulation is still not supported, and then Time.ts was born.

At this time Time.ts does not support manipulating datetime formats but in the future I plan to make a format manipulation.

Basically the way Time.ts works is very simple, just get the current UTC time using new Date () like in normal JavaScript way and from there I get the time in UTC or GMT + 0. Then change it to the milisecond format using getTime ().

let getUTCDate = new Date().getTime()
// we will get current UTC time in milisecond format.
Enter fullscreen mode Exit fullscreen mode

Okay, after getting the current time in UTC, then I will get the current time for my own timezone, which is GMT + 7 or Jakarta time. Because it's GMT + 7, so my time difference with UTC is overlap up to 7 hours or if from my timezone to UTC is having -7 hours of difference, correct?

So programmatically, I need to get my current time offset to UTC with new Date (). GetTimezoneOffset (). Then I will get the difference in my current time to UTC which is -7 * 60 minutes = -420 and from that result I get, I will convert it to the milisecond format so that it can be added up. Oh yes, using getTimezoneOffset will calculate the distance of minutes of your server time to UTC time. So It is server time, not your PC time.

let getUTCDate = new Date().getTime();
// we will get current UTC time in milisecond format.

let TIME_IN_MILISECOND = 60000;

let getTimeDiff = new Date.getTimezoneOffsite();
// -420
let convertToMs = getTimeDiff * TIME_IN_MILISECOND;

let getMyTime = new Date(getUTCDate - convertToMs).toISOString();
//it returns time in my timezone GMT+7

/**
 - Why minus ( - ) ? well actually it's a plus ( + )
    we also can do this.
    convertToMs *= -1
    new Date(getUTCDate + convertToMs)
*/

Enter fullscreen mode Exit fullscreen mode

Then what if I want to get another timezone for example Singapore timezone? We need to see Singapore first which is in GMT + 8, and it has 8 hours of overlap from UTC. It's easy, just add the UTC time in milliseconds with 8 hours in the milisecond format as well.

let SGOverlap = 8 * 60 * 60000;

let UTCTime = new Date().getTime();

let SGTime = new Date(UTCTime + SGOverlap).toISOString();
Enter fullscreen mode Exit fullscreen mode

Time.TS

To use Time.ts simply just import project time.ts.

import { Time, timezone } from "https://denopkg.com/burhanahmeed/time.ts/mod.ts";
//or
import { Time, timezone } from "https://deno.land/x/time.ts/mod.ts";
//or
import { Time, timezone } from "https://denoland.id/x/time.ts/mod.ts";
Enter fullscreen mode Exit fullscreen mode

Then for the example code is as follows.

import { Time } from "https://denopkg.com/burhanahmeed/time.ts/mod.ts";

console.log('Jakarta Timezone: ', new Time().timezone('Asia/Jakarta'))
//Jakarta Timezone:  2020-06-06T20:21:14.765Z

console.log('Singapore Timezone: ', new Time().timezone('Asia/Singapore'))
//Singapore Timezone:  2020-06-06T21:21:14.766Z

console.log('Jakarta Timezone: ', new Time('2020-06-06 14:33:12').timezone('Asia/Jakarta'))
//Jakarta Timezone:  2020-06-06T14:33:12.000Z

console.log('Singapore Timezone: ', new Time('2020-06-06 14:33:12').timezone('Asia/Singapore'))
//Singapore Timezone:  2020-06-06T15:33:12.000Z

console.log('UTC timezone: ', new Time().utc)
//UTC timezone:  2020-06-06T13:21:14.766Z

console.log('Now timezone: ', new Time().now)
//Now timezone:  2020-06-06T20:21:14.766Z
Enter fullscreen mode Exit fullscreen mode

In the future, I hope I have time to develop this module further. If you find a problem, you can submit an issue at github Time.ts.

Also don't forget to try my Denamo boilerplate.

Also if you prefer reading in Bahasa Indonesia you can visit here

Top comments (1)

Collapse
 
terkwood profile image
Felix Terkhorn

I just wanted to say hello, and thank you!

I used this in my work. 🤩

github.com/Terkwood/syn/pull/7