Burhan Uddin
Burhan Uddin

Reputation: 779

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 :)

Upvotes: 34

Views: 54060

Answers (6)

Chris Catignani
Chris Catignani

Reputation: 5306

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

DateTimeOffset Struct

I wrote an extension for this:

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; }
    }
}

Upvotes: 0

Moumit
Moumit

Reputation: 9630

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;
        }

Upvotes: 0

Adnan Sarwar
Adnan Sarwar

Reputation: 139

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.

Upvotes: 12

giammin
giammin

Reputation: 18958

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.

Upvotes: 26

Elkin Cruz
Elkin Cruz

Reputation: 71

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;   
        }

Upvotes: 5

Pratik 1020
Pratik 1020

Reputation: 313

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());

Upvotes: 4

Related Questions