2

I have a problem with difference in Server time and client time.

I am in India, i am storing datetime/timestamp from India (suppose at 14:00) but it is being persisted in database with +5.30 hrs (i.e, if local time is 14:00 it is persisted as 19:30). And while displaying i am getting latter value(i.e: 19:30). This is probably because of the change in server time, server is located in some other country.

I have many filtering functions which uses this time, hence all my functions which uses datetime are gettting affected. How to deal with this situation? Plz give me suggestions.

Flexicoder
  • 8,251
  • 4
  • 42
  • 56
Noor Khan
  • 255
  • 2
  • 3
  • 8

2 Answers2

1

I think the date object is communicated between server and client in milliseconds (EPOCH). When server gets the EPOCH then the time it calculates would be as per its timezone. It would be good to make the date/time communication via String.

So client sends date in a format say "01/01/2014 14:00" (String value), the server reads it as String and parses it (using format MM/dd/yyyy HH:mm) to a date object and persists it. In this way there will be no difference between client and server.

sanbhat
  • 17,522
  • 6
  • 48
  • 64
  • Isn't that quite the opposite way round? EPOCH is alwyas absolute and a String containing _no TZ spec_ is interpreted as local TZ??? – Axel Amthor Mar 13 '14 at 12:33
  • OP wants to persist the time selected in client as it is to the server. with EPOCH, the server will calculate the time based on its current time. But incase of String, there is no calculation, just parsing (conversion) – sanbhat Mar 13 '14 at 12:35
  • To convert EPOCH to a String, you need a given TZ. Based on that TZ, re-converting will result in the same EPOCH, so what is the gain in here? – Axel Amthor Mar 13 '14 at 12:35
  • `But incase of String, there is no calculation` is wrong. – Axel Amthor Mar 13 '14 at 12:36
1

I assume that

(i.e, if local time is 14:00 it is persisted as 19:30)

is related to a mismatch between the time zones of your server and your local system, not related to a difference in absolute time. Due to the mismatch of 5.30 hours, I assume the database is storing in UTC. You need to set the correct TZ on the connection to your database or convert the timestamp given from the DB to the local TZ (whatever that is).

Axel Amthor
  • 10,980
  • 1
  • 25
  • 44