Display local time from utc time stored in sql database on asp net app


When working with an ASP.NET that interacts with a SQL database, it is to encounter scenarios you need to display local time based on UTC time stored in the database. This article will guide you through the process of achieving this using ASP.NET programming language.

Step 1: Retrieve UTC time from the database

To begin, you need to retrieve the UTC time from the SQL database. This can be done using a SQL or an ORM (Object-Relational Mapping) such as Entity Framework. Here's an example of how you can retrieve the UTC time using Entity Framework:

using (var dbContext = new YourDbContext())
    var utcTime = dbContext.YourTable.FirstOrDefault().UtcTime;

Step 2: Convert UTC time to local time

Once you have retrieved the UTC time from the database, you need to convert it to the local time zone of the user. ASP.NET provides the TimeZoneInfo class to handle time zone conversions. Here's an example of how you can convert the UTC time to local time:

var localTimeZone = TimeZoneInfo.FindSystemTimeZoneById("YourLocalTimeZoneId");
var localTime = TimeZoneInfo.ConvertTimeFromUtc(utcTime, localTimeZone);

Step 3: Display the local time on your ASP.NET app

Now that you have the local time, you can display it on your ASP.NET application. This can be done by assigning the local time to a label or any other UI element. Here's an example of how you can display the local time:

lblLocalTime.Text = localTime.ToString();


In this article, we have explored how to display local time from UTC time stored in a SQL database on an ASP.NET application. By following the outlined above, you can that the displayed time is accurate and adjusted to the user's local time zone. to handle any exceptions that may occur during the conversion process and error handling in your application.

Rate this post

Leave a Reply

Your email address will not be published. Required fields are marked *

Table of Contents