Base solution for your next web application
Open Closed

UTC time how to handle user inputs. #3649


User avatar
0
itbmo created

Ok so we have Clock.Provider = ClockProviders.Utc; in startup and it allows tenants/user to have different timezone. My question is how we pass datetime form view to application service, for example if there is an appointment module and user selects particular datetime , Abp will automatically converts datetime to utc at the time of db entry/update ? Or we need to convert the datetime to utc in view and send that to application service ?


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

    Hi @ITBMO,

    If you use UtcClockProvider, ABP will convert given datetime to UTC before it reaches to app service. You need to display datetime to user in user's timezone. This can be done using moment timezone client side libary. You can find sample usages in AspNet Zero.

    Thanks.

  • User Avatar
    0
    itbmo created

    Ok it works as you explained for application service, how about controller ? Sometimes we will have to post form to controller as well, that doesn't work same as application service. For example we are facing issue with "31/08/2017" date, in converts to utc and working proper in application service but in controller it says the date is not correct, obviously we have dd/mm/yyyy date in selected culture.

  • User Avatar
    0
    itbmo created

    Sorry in previous message i was wrong, actually even on app service, it doesn't convert the time to UTC by default, just saves the same time which user selects from view, so if user has different timezone, the date should be converted to UTC automatically but that's not working, do we need to configure something in app service start up as well ? And there is one more issue in controller the date is giving error when we try "31/08/2017" , the same date giving no error in app service.

  • User Avatar
    0
    alper created
    Support Team

    Hi,

    You need to send the date from client in UTC format and save it in UTC. Whenever you want to view the date, just convert it to user's timezone. So everything is in UTC, view is in client's timezone.

  • User Avatar
    0
    itbmo created

    Any example converting user entered datetime to UTC at client side ? I know we need to use moment and abp.timing.timeZoneInfo , can anyone send the exact way of converting datetime to UTC ?

  • User Avatar
    0
    ismcagdas created
    Support Team

    Hi @ITBMO,

    First, sorry for the late reply.

    If you have a moment date object, then you might not need to convert it to UTC. But you can convert any date to utc using moment's format function like this:

    moment(yourDate).format('YYYY-MM-DDTHH:mm:ssZ')
    

    The "Z" postfix in format will format your date in UTC.

    Thanks.