0

I am working on a requirement where I will save the Date of Submission value of a customer. It works perfect. But the problem is, when I fetch that data related to customer and rendering it in the UI, the date of submission is being displayed wrong. For suppose if I save it as 01/01/2019, it displays as 12/31/2018 11:56PM. This happens only when I change the timezone. In my local timezone it works perfect. But, when I change it to some other timezone, it displays like the above or some other value based on the timezone.

I am using Asp.Net MVC, C#.Net. I debugged the application and checked that the date that was being returned from the database is perfect, which is 01/01/2019 12:00AM. But when this is being rendered, it is displaying the incorrect value.

Here is the code that I am using to render the values to UI. I only included this piece of code because whatever I am getting to the model was good. The issue seems to be happening at the time of rendering. Can someone please help me out with where I am going wrong or what can I do to the above code to fix this issue?

<div class="form-group margin-top-10">
    @ProjectHolder.LayoutHForm(new { @class = "v-split-4" }, null,
    x => x.mdl.FullName,
    x => x.mdl.SRNumber,
    x => x.mdl.DateOfSubmission,
    x => x.mdl.IsMale,
    x => x.mdl.EmailAddress)                    
</div>
D Ritz
  • 9
  • 3
user2083386
  • 135
  • 3
  • 19

1 Answers1

0

The browser is displaying the time based on its understanding of the timezone. So it is converting your date to the timezone that your browser detects. First, make sure you are storing your time in UTC format. Now when you display your time, instead of just rendering the raw time, convert that using date format string or available methods. That should display accurate time based on the timezone. You can use methods like ToShortDateString or just DateValue.ToString("dd-mm-yyyy") or any format string suitable for your case.

Shahzad
  • 1,677
  • 1
  • 12
  • 25
  • well to convert to the datetime of the client, youd need to know the timezone the client was in to do it with C#. If the timezone data is not available/being stored then you can store in UTC (as mentioned above) and use javascript to convert to local time and display. https://stackoverflow.com/questions/13622142/javascript-to-convert-utc-to-local-time – GregH May 24 '19 at 16:41
  • Yes, rightly said. If that has to be done in C#, the culture info needs to be sent from the client side. Another option is to do it on the client side using the browser script. – Shahzad May 24 '19 at 16:45