Mihai Alexandru-Ionut
Mihai Alexandru-Ionut

Reputation: 48387

How to save correct time in database?

I have one object called appointment which has two properties: StartDate and EndDate.

When I make POST request I send these values using ISOString time .

this.appointment.StartDate.toISOString()

On the server-side, I received these properties with correct values. Also, it seems to be correct when I create model in order to save appointment to the database. I used .ToUniversalTime() method.

var newAppointment = new Appointment()
{
        StartDate =Convert.ToDateTime(model.StartDate).ToUniversalTime(),
        EndDate = Convert.ToDateTime(model.EndDate).ToUniversalTime(),
        SpecialityId = speciality.Id,
        LocationId = location.Id,
        PatientId = patient.Id,
        UserId = user.Id,
        Observations = model.Observations
};

But in database I found another values. Can explain somebody why is this behaviour ?

For instance, I used 2017.09.01 11:00 for StartDate and in database i found 2017-09-01 08:00

The server and database is located in the westeurope.

Upvotes: 0

Views: 3942

Answers (2)

Matt Johnson-Pint
Matt Johnson-Pint

Reputation: 241603

A few things:

  • Don't call ToUniversalTime in a web application. It's designed to convert from the server's local time zone to UTC. The server's time zone should be irrelavent to your application. Web applications should never use ToUniversalTime, ToLocalTime, DateTime.Now, TimeZoneInfo.Local, DateTimeKind.Local or any other method that uses the time zone of the computer it's running on.

  • Ideally, on the server side, your model.StartDate and model.EndDate would already be DateTime objects, because they'd have been deserialized that way. Therefore, you probably don't need to call Convert.ToDateTime. If they are strings, then I would adjust your model class accordingly.

  • On the client side, assuming StartDate and EndDate are JavaScript Date objects, and they were created using local time values (that is, the time zone of the browser), when you call toISOString, you're not just getting a string in ISO 8601 format - it is also converting it from the browser's time zone to UTC.

  • In your example, the UTC time is 3 hours ahead of UTC for the date and time shown. From your profile, I see you are located in Romania, which is indeed UTC+3 for this date, because it is currently observing Eastern European Summer Time. When Summer Time ends (on October 29, 2017 at 04:00), it will return to UTC+2. For this reason, you cannot simply add three hours to all values.

  • If you want to send local time values from the client, you should send them in ISO 8601 format, without any Z or offset, for example 2017-09-01T11:00. There are several ways to achieve this:

    • The best way is to not have them in a Date object to begin with. For example, if your input uses the <input type="datetime-local" /> input type (as specified in HTML5), the .value property is not a Date object, but rather a string in ISO 8601 format.

    • If you can't avoid a Date object, then create a local ISO string, like this:

      function dateToLocalISOString(date) {
          var offset = date.getTimezoneOffset();
          var shifted = new Date(date - offset * 60 * 1000);
          return shifted.toISOString().slice(0, -1);
      }
      

      OR, using Moment.js:

      moment(yourDateObject).format("YYYY-MM-DD[T]HH:mm:ss.SSS")
      
  • Lastly, you will probably read advice from others about storing these as UTC. Don't listen. The advice "always use UTC" is shortsighted. Many scenarios require local time. Scheduling appointments is a primary use case for local time. However, if you need to act on that appointment, you'll use the current UTC time, and you'll also need some information about the time zone for the appointment so you can convert from UTC to the appointment's time zone. For example, if this is something like an in-person doctor's office appointment, then it's safe to assume the time zone of the doctor's office. But if it's an appointment for an online meeting, then you'll have to capture the user's time zone separately and apply it on the back end where appropriate.

Upvotes: 2

Swoox
Swoox

Reputation: 3740

The problem is with your current timezone.

What your application does is get current timezone (+3) in this case.

Now it got your timezone but it will convert to utc time. So what will happen, your current time will be -3 hours.

If you not adjust to summer and winter time then you can simply add 3 hours to the datetime. Otherwise you have to get the offset of your timezone and add that to the current datetime value. Take care if you use this application in different timezones. For example You life in +3 and some else life in +2 timezone.

Upvotes: 0

Related Questions