简体   繁体   中英

Server side changes date/time sent by client

I'm facing the following problem, I have a play application with angular and java. In one page the client selects in a calendar some date and time for example 2015-04-03 15:00 this data is put in a JavaScript object as a Date and later this data is submit to my server but it seems the server is converting this date/time to his timezone saving 2015-04-03 16:00 instead of 15:00 as the client side sent.

After I submit the data to the server where it is persisted in the database when I reload the page shows the date with 1 hour less

Sending data to server. Notice that there is a console.info() that prints the date time. It's printing the correct date/time. The date/time selected by user.

$scope.confirmCallback = function () {
                $scope.schedule.client = $scope.client;
                $scope.schedule.type = 'CONTACT';
                console.info($scope.schedule.date); //PRINTS OK DATE/TIME
                ScheduleRepository.create($scope.schedule).then(function () {
                    Alert.success('views.schedule.message.successSaved');
                    $scope.schedule = {};
                    $scope.tableSchedules.reload();
                }, function () {

                });
            }

Here is on my server side on a controller that receives the request. The moment the request gets on the server if I inspect the json I can see that the date time value is different from the one I sent. I guess is something related to timezone on client side and server side.

@Dynamic("CREATE_SCHEDULE, EDIT_SCHEDULE")
public static Result save() {
    try {
        JsonNode request = request().body().asJson();//SHOWS DIFFERENT DATE/TIME
        ScheduleClient scheduleClient = JSONUtils.fromJson(request, ScheduleClient.class);

Any suggestions how to solve this problem? Thanks in advance

A couple of things to realize:

  • A Date object can't be sent across the wire. It has to be serialized .
  • There's no native date serialization format for JSON, but the best-practice convention is to send an ISO-8601 / RFC3339 serialized string.
  • The JS Date object takes on the time zone of where it is running. So if you call toISOString on it (or if your ScheduleRepository does), it will be converted to UTC using that time zone.
  • On the receiving end, your JSONUtils.fromJson call will deserialize the string value back to whatever object structure your ScheduleClient class uses.
  • If that object also takes on local time behavior, it will use the local time zone of the server .

So you are either seeing the time difference due to comparing local values to UTC values, or by comparing local time values to another time zone's local time.

It's difficult to give more exact advice on what you should do, as you didn't show the important parts of your code. We would need to see the original assignment of the Date object, the serialization code, the string value as sent over the wire, the deserialization code, and the class structure that was being deserialized into. We would also need some context to understand whether your user is selecting a date and time at a particular universal instant, or a particular local-by-their-timezone date and time, or just a calendar date, or what. Context is key, and you haven't provided much to go on.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM