How to change time zone for an asp.net application

I need to set default timezone for my ASP.NET to Asia/Dhaka or GMT+6 timezone. But i cannot find a way to change it globally. There is a lot of reference on Stackoverflow and rest of the web for doing this by getting timezone info and calculating correct time for each time i need a DateTime object.

But trust me, I don’t want to do this in this way. So dont give me any suggestion like that. I want to set the timezone to Asia/Dhaka or GMT+6 preferably from web.config. (Similar we do in php with php.ini) So that each time i need DateTime object the time is evaluated with my timezone no matter what the timezone is for server.

Is this possible? If possible then how??
Thanks in advance for the solution 🙂

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

Sorry there is no way in .NET to change the time zone globally.

The only way you have is to change the timezone of your server or rewrite all of your code.

The best practice is to not rely on the system time zone at all (never use DateTime.Now).

You should handle all date as Utc dates and then convert to a specific zone when displaying them to users.

Even if you manage to handle timezones in your ASP.NET application, there are still timezones on SQL Server, for example GETTIME funtion. If your application is entirely written in UTC, your SQL server function will work as well.

Method 2

There’s very easy way to do it. Simply get the current UTC time and your timezone in two different variables. Then convert UTC to your timezone in third variable and use it anywhere. Here’s how you do it.

DateTime date1 = DateTime.UtcNow;

TimeZoneInfo tz = TimeZoneInfo.FindSystemTimeZoneById("Pakistan Standard Time");

DateTime date2 = TimeZoneInfo.ConvertTime(date1, tz);

Set your Time Zone in tz and then use “date2” anywhere.

Method 3

You can Change TimeZone…And Get Date

 DateTime utcTime = DateTime.UtcNow;
    TimeZoneInfo myZone = TimeZoneInfo.FindSystemTimeZoneById("India Standard Time");
    DateTime custDateTime = TimeZoneInfo.ConvertTimeFromUtc(utcTime, myZone);
    Str.Append(custDateTime.ToString());

Method 4

I have a problem with the instruction:

TimeZoneInfo.FindSystemTimeZoneById("India Standard Time");

So… i create a personal TimeZoneInfo.

Here my code…

public static DateTime DateNow()
        {
            DateTime utcTime = DateTime.UtcNow;
            TimeZoneInfo myZone = TimeZoneInfo.CreateCustomTimeZone("COLOMBIA", new TimeSpan(-5, 0, 0), "Colombia", "Colombia");
            DateTime custDateTime = TimeZoneInfo.ConvertTimeFromUtc(utcTime, myZone);
            return custDateTime;   
        }

Method 5

Having a similar issue (with Time zones) I ended up changing some database types from DateTime to DateTimeOffset (MSSql).

DateTimeOffset Struct

Wrote an extension

public static class Extensions
{
    public static DateTimeOffset ToCentralTime(this DateTimeOffset value)
    {
        return TimeZoneInfo.ConvertTime(value, TimeZoneInfo.FindSystemTimeZoneById("Central Standard Time"));
    }
}

Example of use:

public class ClockPunch
{
    private DateTimeOffset dtoTimeIn;
    private DateTimeOffset? dtoTimeOut;
    public ClockPunch() { }
    public DateTimeOffset TimeIn
    {
        get { return dtoTimeIn.ToCentralTime(); }
        set { dtoTimeIn = value; }
    }
    public DateTimeOffset? TimeOut
    {
        get
        {
            DateTimeOffset? retVal = null;
            if (dtoTimeOut != null)
            {
                retVal = ((DateTimeOffset)dtoTimeOut).ToCentralTime();
            }
            return retVal;
        }
        set { dtoTimeOut = value; }
    }
}

Method 6

Better could be to create a customTimeZone

        public static DateTime DateNow()
        {
            DateTime utcTime = DateTime.UtcNow;
            TimeZoneInfo myZone = TimeZoneInfo.CreateCustomTimeZone("INDIA", new TimeSpan(+5, +30, 0), "India", "India");
            DateTime custDateTime = TimeZoneInfo.ConvertTimeFromUtc(utcTime, myZone);
            return custDateTime;
        }


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x