Erick Gallani Erick Gallani - 2 months ago 16
AngularJS Question

Working with UTC and current time zone

I'm working on a project that was used only in one country but now is in using in several countries.

So I'm working in some DateTime issues, as you can image.

I'm using angular js for my frontend, python for my backend and Postgres as my database in this project.

To avoid any problem with DateTime and try to make more easy to work with the timezones I'm saving the DateTime in the database as UTC.

from DateTime import DateTime

# inside a class of my entity
self.start_date = datetime.utcnow()


This is working fine, the problem is when I try to convert the date back.

For example.


If my application is running in a country with GMT -1, when the user
asks to save the entity and it's 2016-07-13 15:00:00, in the database
(using the UTC now()) the DateTime will become 2016-07-13 16:00:00.


But when I try to get back the value I have two scenarios:


  1. If I don't do anything, I'll receive the DateTime like it's on the database, "2016-07-13 14:00:00"

  2. If I try to convert to the local timezone, I'm getting like 2016-07-13 17:00:00. The time was increased by 1 and not decrease was I expected.



I'm trying to use the momentjs library to work with dates, but nothing seems to work.

I'm wondering if what I supposed to do is get the GMT, like (-01:00) then do some math with the DateTime that comes from the database, like sub or sum the GMT hours difference.

Answer

Solution:

My solution for this was, store everything as UTC in my database.

Retrieve the datetime as UTC and covert to browsers timezone using momentjs library.

Doing like this.

moment.tz(moment.utc(datetime), moment.tz.guess());