Converting UTC to Local Time

Penny Pang
Graduation Thesis 2019
2 min readNov 3, 2019

The API was working, the geolocation was working, the web app was outputting the data to know what stops to catch the bus from and where the stops are. Huge problem though was that I did not know when the bus will be coming? (Essentially the whole point of the app)

The app was meant to give a one click solution to inform the transport mode is coming in. The only problem was that the output of the time was in UTC.

arrivalTimeEstimated is the real-time data

arrivalTimePlanned is the planned time that the train was suppose to come

But what even is 2019-11-03T03:38:36Z ? You may ask. UTC time.

Coordinated Universal Time is the primary time standard by which the world regulates clocks and time.

So I had to convert UTC time to AEST time.

How to convert time difference?

I was introduced to moment.js which allows you to parse, validate, manipulate, and display dates and times in JavaScript.

momentparse() parses the string but keeps the resulting Moment object in a fixed-offset timezone with the provided offset in the string. I extracted the time from the trip API result and stored it in a constant time then I use moment to get the local time

let localTime = moment.parseZone(time);

to get the current server time, you just need to do let now = moment();

We don’t care about what time the train is coming, but we want to know how many minutes left until the train comes. To do that, we can use the diff

const timeEstimate = parseInt(localTime.diff(now)/1000,10);

You can then update the state that you have previously initialised in the constructor method

this.setState({ time: timeEstimate });

--

--

Penny Pang
Graduation Thesis 2019

Full-time UX/UI Designer, Part-time online entrepreneur, Casual food blogger and innovation advocate