Base solution for your next web application
Open Closed

How to send Dates to Server and return them back to client #2659


User avatar
0
bilalhaidar created

Hi, I am a bit stuck here.

When sending DateTime values to server, should I do anything special on client side? Like convert the dates to some format or use moment or ?

On the server, I can then use Clock.Normalize(Convert.ToDateTime('Date From Client')) to store the value in UTC in the DB?

On some entities, when I return them to client I notice the CreationTime is "2017-03-09T00:41:42.647Z" for example. I guess, the CreationTime is stored in UTC, so how could I return my values from server in that format?

I am a bit confused in here.

Appreciate your help.

Bilal


8 Answer(s)
  • User Avatar
    0
    ismcagdas created
    Support Team

    Hi,

    Actually when returning data from server to client and vice versa, ABP handles date time normalization. You need to choose which Clock Provider (UTC, Local, Unspecified) to use.

    When sending data from client to server, you can format your date with moment, we do it in that way in our projects.

  • User Avatar
    0
    bilalhaidar created

    Thanks Ismail :)

    Can you refer me to a sample code where you use moment to prepare date before sending it to server, other than AuditLog?

    Regards Bilal

  • User Avatar
    0
    ismcagdas created
    Support Team

    Hi,

    As far as I remember, we used it only in audit logs. Please ask if there is an unclear point for you for the usage in audit logs.

  • User Avatar
    0
    bilalhaidar created

    Sure, I will check it and get back to you. Many thanks

  • User Avatar
    0
    bilalhaidar created

    Hi Ismail,

    Seems the Date issue is still not clear to me. I checked the audit logs, the idea is not clear there.

    When sending datetime from client to server, what shall i do to the date? Always make sure it is sent as per client timezone? How to do so in Abp?

    On submitting a form, on server, Abp based on configured provider to normalizes the date input?

    Thanks

  • User Avatar
    0
    ismcagdas created
    Support Team

    Hi,

    I assume you are using UtcClockProvider in your application.

    The idea is simple actually. You send a date from client to server, it does not matter if it is UTC or not, ABP normalizes this date with Clock.Normalize. If you are using UtcClockProvider, it converts given date to UTC, if it is already UTC then it does not do anything.

    When you are sending a date from server to client, same thing happens. So in your case client side gets an UTC date. In the client side we use momentjs to show dates.

    We also use moment timezone library and set it's timezone to user's current timezone and momentjs displays dates in the user's timezone.

    So in this case Sending 2017-03-22T05:00:00+04:00 or 2017-03-22T03:00:00+02:00 or 2017-03-22T01:00:00Z will be the same.

  • User Avatar
    0
    bilalhaidar created

    Thanks for the explanation.

    Can you show me in the code where this conversion is happening on both, the client side and server side?

    Thanks

  • User Avatar
    0
    ismcagdas created
    Support Team

    Hi,

    It is done in ABP, following links are for MVC and WebApi <a class="postlink" href="https://github.com/aspnetboilerplate/aspnetboilerplate/blob/master/src/Abp.AspNetCore/AspNetCore/Mvc/ModelBinding/AbpDateTimeModelBinder.cs">https://github.com/aspnetboilerplate/as ... lBinder.cs</a> <a class="postlink" href="https://github.com/aspnetboilerplate/aspnetboilerplate/blob/master/src/Abp.Web.Api/WebApi/Controllers/Dynamic/Binders/AbpApiDateTimeBinder.cs">https://github.com/aspnetboilerplate/as ... eBinder.cs</a>