Share via

Incorrect Time from database table column Date_and_Time, with "datetime" DataType

Donald Symmons 3,096 Reputation points
2022-12-03T11:50:52.817+00:00

I have an issue with time saved in the database. It does not give accurate time with my local time. If I insert data into the table with DateTime.Now, I thought it should get the time automatically in the local time and insert, so that when it displays, it will show exactly the time an event occurred.
Here is how I inserted the date and time with DataType: datetime

objCMD.Parameters.Add("@LastLogin", SqlDbType.DateTime, 100).Value = DateTime.Now;  

OR

cmd.Parameters.AddWithValue("@LastLogin", DateTime.Now);  

Then, displaying the date and time I use

timelbl.Text = Convert.ToDateTime(Session["LastLogin"]).ToString();  

or when displaying in the Gridview I use

<asp:BoundField DataField="LastLogin" HeaderText="Date and Time" HeaderStyle-Font-Bold="false" />  

Please what can I do to make the date and time work according to the real time local time and according to the machine time?

Developer technologies | ASP.NET | Other
SQL Server | Other
Developer technologies | C#
Developer technologies | C#

An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.


2 answers

Sort by: Most helpful
  1. Dan Guzman 9,436 Reputation points
    2022-12-03T13:32:01.757+00:00

    I see an 8 hour difference with my local time. I expected to see a time same as my local time

    @Donald Symmons , it seems the web server time zone is different than as your local time zone, with a difference of 8 hours. DateTime.Now gets the local time where the code runs. An 8 hour difference is expected if, for example, your machine is Pacific Standard Time and the web server time zone UTC.

    As suggested, you could store the datetime value in UTC (neither .NET nor SQL Server datetime types are time zone aware) and then convert the time to the time zone of your choosing for display purposes. Similarly, you could use datetimeoffset instead which will include the offset with DatetimeOffset.Now.

    It can be a tricky to determine the end client time zone or offset on the server side to display the time in your local time. For that reason, consider formatting the value in client-side javascript from an ISO-8601 datetime format source.


  2. AgaveJoe 31,266 Reputation points
    2022-12-03T12:01:33.703+00:00

    It does not give accurate time with my local time.

    Datetime.Now gets the current date and time zone on the web server not the client machine.

    Please what can I do to make the date and time work according to the real time local time and according to the machine time?

    Use UTC time and store the user's time zone.

    https://stackoverflow.com/questions/832986/how-to-work-with-time-zones-in-asp-net


Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.