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 an
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());
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;
}
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;
}
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.
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.
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; }
}
}