Default value for datetime

10 posts, 0 answers
  1. Robert
    Robert avatar
    41 posts
    Member since:
    Aug 2015

    Posted 18 Nov 2016 Link to this post

    We have a server in UK, and our clients are in US with different timezones.

     

    In a grid, we set up a datetime field with default value as datetime.now in the cshtml view file. However, we found the issues:

     

    1. The datetime.now is server datetime, not client datetime, so it is always wrong. how can we use the users datetime value?

     

    2. we have tried using onsave event with gridrow.isnew() to set client's default value. however, if the user change the value, the isnew() is still return true which overwrites user's  new input.

     

    Thanks

  2. Viktor Tachev
    Admin
    Viktor Tachev avatar
    2500 posts

    Posted 21 Nov 2016 Link to this post

    Hi Robert,

    In order for the dates to be validated and updated correctly you need to ensure that you set matching cultures on the server and on the client. Please examine the following thread that describes how you can ensure that the cultures on the server and client are the same.



    Regards,
    Viktor Tachev
    Telerik by Progress
    Telerik UI for ASP.NET MVC is ready for Visual Studio 2017 RC! Learn more.
  3. Robert
    Robert avatar
    41 posts
    Member since:
    Aug 2015

    Posted 23 Nov 2016 in reply to Viktor Tachev Link to this post

    Hi Viktor,

    We cannot set up the cultures for the server side because this is a global app. The reason is that, the datetime.toString is depending on the user's login culture setting. eg.

    • user A is in UK, it is "31 Dec 2016" and the timezone is in UK
    • user B is in Japan, it is "2016/12/31" and the timezone is in Japan
    • user C is in US, it is "12/31/2016" and the timezone is depending on their states (computer time)
    • user D is from US, it is "12/31/2016" but he is traveling around different timezone.

    Currently, we have a patch process in cshtml to format the datetime correctly based on user's culture profile. However, when we use .default(DateTime.Now) in the kendogrid column setting, the value is not right for the users in a different timezone. The user want to see their machine time instead of server's current time. How can I make new added column using the user's machine time? (as I describe earlier, I use onSave to trigger JS  by checking gridrow.isnew(). However, if I have 3 rows new rows, all values are changed . I want the only added row to be changed for user's current time.

     

     

     

     

     

     

  4. Viktor Tachev
    Admin
    Viktor Tachev avatar
    2500 posts

    Posted 24 Nov 2016 Link to this post

    Hello Robert,

    In that case you should set the culture as described in the Per-request setup section of the article. Override the Initialize method for the controller and set the appropriate culture that will be used.

    Regards,
    Viktor Tachev
    Telerik by Progress
    Telerik UI for ASP.NET MVC is ready for Visual Studio 2017 RC! Learn more.
  5. Robert
    Robert avatar
    41 posts
    Member since:
    Aug 2015

    Posted 24 Nov 2016 in reply to Viktor Tachev Link to this post

    thanks

     

    However, how does this help us to get the client current datetime value by culture for new row?? I am in California and in new York are the same culture but different time values.

  6. Viktor Tachev
    Admin
    Viktor Tachev avatar
    2500 posts

    Posted 28 Nov 2016 Link to this post

    Hello Robert,

    In that case you can keep the dates in the database in UTC format. You should convert the time manually before sending it to the server to ensure that is stored in the correct format.

    Please check out the following example that illustrates the approach:


    Also, examine the thread below that discusses similar behavior.



    Regards,
    Viktor Tachev
    Telerik by Progress
    Telerik UI for ASP.NET MVC is ready for Visual Studio 2017 RC! Learn more.
  7. Robert
    Robert avatar
    41 posts
    Member since:
    Aug 2015

    Posted 28 Nov 2016 in reply to Viktor Tachev Link to this post

    Hi Viktor,

     

    yes, we have all UTC set up correct ly, the only issue is the value

     for example, in cshtml , we use default datetime.now do one column,  the UK users see the value for the new row is California web server current time value.

    Eg,  the web server is 5:55 now in US, but UK is 17:55 already, but he still see 5:55 in his machine, when he creates a new record.

    Thanks

     

     

     

  8. Robert
    Robert avatar
    41 posts
    Member since:
    Aug 2015

    Posted 28 Nov 2016 in reply to Robert Link to this post

    screenshot of cshtml file
  9. Robert
    Robert avatar
    41 posts
    Member since:
    Aug 2015

    Posted 28 Nov 2016 in reply to Robert Link to this post

    well.. I needed that feature now.. so I end up hacking it in the JS, but still looking for a better way (in cshtml) instead of hacking the default value afterwards in JS.

     

    if (e.model.isNew() == true && e.model.isNewInerted == null) { //replace to use the client Now
        e.model.isNewInerted = false;
        e.model.appointmentDatetime= Date.now();
    }
  10. Viktor Tachev
    Admin
    Viktor Tachev avatar
    2500 posts

    Posted 30 Nov 2016 Link to this post

    Hello Robert,

    In order to use the user's local time you need to use client-side logic to create the date. Note that each JavaScript Date object is created with timezone offset by default. Then, you need to account for that offset and adjust the time before sending the data to the server.

    Regards,
    Viktor Tachev
    Telerik by Progress
    Telerik UI for ASP.NET MVC is ready for Visual Studio 2017 RC! Learn more.
Back to Top