The problem
I have hosted a Razor pages web app on a cloud server which happens to be on GMT-8 and when I save data is uses the server side time. The client side datetime is on GMT+1.
Question
How can I change the default datetime value inside the app, so that it matches the client side?
I mean that when I use DateTime.Today
, it should use the GMT+1 automatically (I don't want to use .AddHours(+9)
because it causes problems in data viewing)
Further explanation
The server helpdesk, advises us that to make this possible I have to insert the code below, somewhere in the app, but I don't know where.
Is there a simpler or better way to do this, without javascript, with just C#?
The code suggested by server's helpdesk:
<script language="C#" runat="server">
protected DateTime GetCurrentTime()
{
DateTime serverTime = DateTime.Now;
DateTime _localTime = TimeZoneInfo.ConvertTimeBySystemTimeZoneId(serverTime, TimeZoneInfo.Local.Id, "Central Europe Standard Time");
return _localTime;
}
protected void Page_Load(object sender, EventArgs e)
{
Response.Write(GetCurrentTime());
}
</script>