Base solution for your next web application
Open Closed

TimezoneHelper.ConvertFromUtc #9071


User avatar
0
ryan.mennell created

I'm using ASP.NET Core MVC & jQuery Project

Below code works correctly, but i always need to use TimezoneHelper.ConvertFromUtc to convert utc date time from server to Client's set Timezone Date time. If i not convert it then it always shows utc date time as it is from server into datetimepicker.

Is There anything that datetimepicker convert this automatically and display client timezone's datetime without converting it in cshtml views?

I have set ClockProviders.Utc in Configure method of startup.cs file as below:

//Initializes ABP framework.
app.UseAbp(options =>
{
options.UseAbpRequestLocalization = false; //used below: UseAbpRequestLocalization
Clock.Provider = ClockProviders.Utc; // Add the clock setting
});

App/Views/SalesEnquiry/ManageSalesEnquiry.cshtml file

@{
var currentTimezone = await SettingManager.GetSettingValueAsync(TimingSettingNames.TimeZone);
Model.SalesEnquiry.EnquiryDate = TimezoneHelper.ConvertFromUtc(Model.SalesEnquiry.EnquiryDate, currentTimezone).Value;
}
<label>@L("EnquiryDate")</label>@Html.TextBox("EnquiryDate", Model.SalesEnquiry.EnquiryDate, new { @class = "form-control m-input", required = "required", autocomplete = "off" })<span class="input-group-text"><span class="far fa-calendar-alt"></span></span><br>

/ManageSalesEnquiry.js file

$('#EnquiryDate').datetimepicker({
locale: abp.localization.currentLanguage.name,
format: 'L LT'
});

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

    Hi @ryan.mennell,

    Can you check if this line https://github.com/aspnetzero/aspnet-zero-core/blob/dev/aspnet-core/src/MyCompanyName.AbpZeroTemplate.Web.Mvc/wwwroot/Common/Scripts/librarySettings.js#L5 is hit when you refresh the page when you put a debugger here ?

    And also share value of abp.timing.timeZoneInfo.iana.timeZoneId if it is hit ?

  • User Avatar
    0
    ryan.mennell created

    Thanks for the quick response. Apologies for the delay with mine.

    Yes it is hitting and value of abp.timing.timeZoneInfo.iana.timeZoneId is "Europe/London"

    Host timezone i set is Default [UTC]. Tenant timezone i set is GMT Standard Time.

    Enquiry Date value in tenant database is 2020-05-25 08:00:00.0000000. which will be considered as UTC as i set ClockProviders.UTC.

    However, On tenant login > enquiry page > DateTimePicker field > i get same date time "25/05/2020 08:00" if i do not convert it manually with "TimezoneHelper.ConvertFromUtc".

  • User Avatar
    0
    ryan.mennell created

    Just bumping this fo visibility.

  • User Avatar
    0
    ryan.mennell created

    Apologies for bumping this again, I can clearly see there is high number of support tickets.

  • User Avatar
    0
    ismcagdas created
    Support Team

    Hi @ryan.mennell,

    Sorry for the delay. Could you send your project via email to [email protected] ? We can fix this problem or offer you a better solution rather than using TimezoneHelper.ConvertFromUtc.

    Thanks,

  • User Avatar
    0
    ryan.mennell created

    I have sent the project via email. Could you get back to me asap.

    Thanks Ryan

  • User Avatar
    0
    ryan.mennell created

    Hello,

    I have recently purchased this and unable to fix this issue on my own.

    I have sent the project via email with zero response (9 Days ago).

    Could you please get back to me ASAP.

    Kind Regards Ryan

  • User Avatar
    0
    Mazharmig created

    Hi Support Team, Any update on this? I am facing the same issue.

  • User Avatar
    0
    atlscoob created

    I'm also dealing with the exact same issue. I have the same setup as the original poster.

    I never noticed it before as I had always asumed the functionality was working as documented. However in testing this week it was discovered that the date in the client view is not being converted from UTC. In fact when the user enters a date via an input or the datetimepicker, the new local datetime is exactly what is then stored in the database. So in short there is zero conversion going on for some reason.

    The conversion/formatting works fine when displayed in datatables, but not in an input within a form.

    I have also confirmed the line above is being hit and returning "America/Chicago" - is anyone working on a solution for this issue?

  • User Avatar
    0
    ismcagdas created
    Support Team

    Hi @atlscoob,

    Do you use jQuery version or Angular version ?

    Thanks,

  • User Avatar
    0
    atlscoob created

    I'm using the jQuery version 6.8.0 is the last version we upgraded to I believe.

  • User Avatar
    0
    Mazharmig created

    I am also using jQuery version.

  • User Avatar
    0
    ryan.mennell created

    Could you please update me on this issue? I have also sent emails with zero answers.

  • User Avatar
    0
    musa.demir created

    Hi @ryan.mennell

    You can add new function something like "ConvertToUserTimezone" to https://github.com/aspnetzero/aspnet-zero-core/blob/dev/aspnet-core/src/MyCompanyName.AbpZeroTemplate.Web.Mvc/Views/AbpZeroTemplateRazorPage.cs and use it. It will be accessable from all razor page

    I also create an issue about that: https://github.com/aspnetzero/aspnet-zero-core/issues/3379 (radtool generated pages should handle it automatically)

  • User Avatar
    0
    ryan.mennell created

    @derirmusa

    Thank you, I will test this and let you know if I have any issues.

  • User Avatar
    0
    atlscoob created

    What exactly is it we should be adding to that class? I'm not exactly clear as to what the function should be doing. Also once the function is added, when is it supposed to be called - from the controller when the view is generated?

  • User Avatar
    0
    ismcagdas created
    Support Team

    Hi @atlscoob,

    You should create a method like this;

    public async Task<DateTime> ConvertToUserTimezone(DateTime date ){
        var currentTimezone = await SettingManager.GetSettingValueAsync(TimingSettingNames.TimeZone);
        rerturn TimezoneHelper.ConvertFromUtc(date, currentTimezone).Value;
    }
    

    and call it in your cshtml file like below;

    <label>@L("EnquiryDate")</label>@Html.TextBox("EnquiryDate", ConvertToUserTimezone(Model.SalesEnquiry.EnquiryDate), new { @class = "form-control m-input", required = "required", autocomplete = "off" })<span class="input-group-text"><span class="far fa-calendar-alt"></span></span><br>
    
  • User Avatar
    0
    Mazharmig created

    It means I have to do this on every view, ANZ is not handling this by default.

  • User Avatar
    0
    ismcagdas created
    Support Team

    Hi @Mazharmig,

    Yes, correct. Sometimes you might want to show the DateTime without modifying it. So, this is the best way.