Click here to Skip to main content
15,885,278 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more: , +
when I retrieve it by using .Tolocal() , it returns server local time, not client time. I save my DateTime column after converting it into UTC.

solution required for
utc time to client local time instead of server local time

What I have tried:

<asp:templatefield>
<itemtemplate>
<asp:label runat="server" id="message1" fontstyle="Georgia"
="" text="<%# Eval("username") +":" + " " + " " + " " +" " + " " + Convert.ToDateTime(Eval("Date")).ToLocalTime()%>" font-bold="True" font-italic="True">
<asp:label runat="server" id="message" htmlencode="false"
="" text="<%# Eval("message").ToString().Replace("\n", "<br/>") %>">

<controlstyle width="670px">
<headerstyle backcolor="#6699FF">
<itemstyle width="670px">
Posted
Updated 11-Apr-20 22:40pm

1 solution

Of course it converts to server local, since the code runs on the server. You need to capture the client's timezone in the browser code, and pass that to the server, or use the Javascript functions: JavaScript Date Reference[^].
 
Share this answer
 
Comments
Member 14192879 12-Apr-20 4:47am    
var now = new Date("January 02, 2012 22: 00: 00 GMT + 0530");
var nowUtc = new Date(now.getTime() + (now.getTimezoneOffset() * 60000));
var nowmillis = now.getTime() + (now.getTimezoneOffset() * 60000);
var nowutcmillis = nowUtc.getTime() + (nowUtc.getTimezoneOffset() * 60000);
var backnow = now.setTime(millis - (now.getTimezoneOffset() * 60000));
var backutc = nowUtc.setTime(millis - (nowUtc.getTimezoneOffset() * 60000));
document.getElementById("demo3").innerHTML = now;

I used this code to retrieve date to localtime and it works well


but I do not know how I can do this gridview date
Richard MacCutchan 12-Apr-20 4:51am    
Why not pass the GMT offset value back to the server? That way it can handle any time zone.
Member 14192879 12-Apr-20 5:17am    
yes I saved date into the database in GMT Format

I want to know how I can pass this javascript on eval("date")


how can I pass this GMT offset to grid view?
Richard MacCutchan 12-Apr-20 5:32am    
Sorry I have not used ASP grid view controls. I suggest you check the documentation to see what properties and methods are available.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900