Take the 2-minute tour ×

I need to pick a future date from calendar, suppose the date I am selecting is 04/30/2013. Now what I want is to send the date to server. At server end I need to calculate the number of days between the future date and current date and send it to database.

The problem is that when I do the calculation locally (because my server and browser are in same timezone) it works fine, but when the server is in a different timezone than the browser the difference in days does not come as expected. Someone please help me how to solve the timezone issues.

share|improve this question
Why don't you substract the dates on the client? –  NeplatnyUdaj Apr 17 '13 at 9:23
How do you send the date to the server? What format, I mean? –  MaxArt Apr 17 '13 at 9:24
@user2266098 That would be a violation of the MVC paradigm. –  MaxArt Apr 17 '13 at 9:25
Your question is not entirely clear to me: you are using javascript on the client side and java on your server ? –  jeroen_de_schutter Apr 17 '13 at 9:26
Well, you could simply pass the utc time to the server which generates the dates and calc the days between. –  sk2212 Apr 17 '13 at 9:28
add comment

2 Answers

Convert both dates in one common timezone e.g. GMT and then calculate the difference.

share|improve this answer
add comment

To Offset for time differences

You should use a format to send the data that includes the timezone within it. You could:

  1. Use UNIX time which does not use timezones (milliseconds since epoch) with GMT 00, and also use this on java side

see: Get current date/time in seconds?

  1. Use ISO-8601 which is a standard and can include the timezone as well of the browser, and then parse this server side:


To Calculate the Difference (using MS and Calendar Object)

You need to use the calendar object. You create a calendar object and set with a future date: e.g. How to set Java.util.calendar to a specific time period in the future Then you can perform a calculation by subtracting the current date (as long) with the long returned by the calendar, and dividing by (1000*60*60*24).

Some code I used in my own application to find entries greater than a specific datetime (midnight):

long currentTimeStamp = System.currentTimeMillis();
Calendar cal;
cal = Calendar.getInstance();
cal.setTime(new Date(currentTimeStamp));

cal.set(Calendar.HOUR_OF_DAY, 0); cal.set(Calendar.MINUTE, 0); cal.set(Calendar.SECOND, 0); cal.set(Calendar.MILLISECOND,0);
long midnight = cal.getTimeInMillis();
if(currentTimeStamp - midnight > (30*24*60*60*1000))break;
share|improve this answer
add comment

Your Answer


