C#: Making sure DateTime.Now returns a GMT + 1 time


C#: Making sure DateTime.Now returns a GMT + 1 time



I am using DateTime.Now to show something according to today's date, and when working locally (Malta, Europe) the times appear correctly (obviously because of the Time Zone) but ofcourse when I upload it to my hosting server (USA), DateTime.Now does not represent the correct time zone.

Therefore, in my code, how can I convert DateTime.Now to correctly return the time from a GMT + 1 timezone ?




adding one day to a date

1:



How to get a Long Date format from DateTime without a weekday
Use the TimeZoneInfo class found in System.Core;.
Calculating time blocks from given date range
You must set the DateTimeKind to DateTimeKind.Utc for this..
How do I format a DateTime? in c#?
DateTime MyTime = new DateTime(1990, 12, 02, 19, 31, 30, DateTimeKind.Utc);  DateTime MyTimeInWesternEurope = TimeZoneInfo.ConvertTimeBySystemTimeZoneId(MyTime, "W. 


Convert.ToDateTime , any way to depict month and year fields C#?
Europe Standard Time");
Only if you're using .Net 3.5 though!.
Convert SQL server datetime fields to compare date parts only, with indexed lookups


SQL Server 2005 Date Time stamp Query


How do you get the current time of day?

2:


It depends on what you mean by "a GMT + 1 timezone".

Do you mean permanently UTC+1, or do you mean UTC+1 or UTC+2 depending on DST?. If you're using .NET 3.5, use TimeZoneInfo to get an appropriate time zone, then use:.
// Store this statically somewhere TimeZoneInfo maltaTimeZone = TimeZoneInfo.FindSystemTimeZoneById("..."); DateTime utc = DateTime.UtcNow; DateTime malta = TimeZoneInfo.ConvertTimeFromUtc(utc, maltaTimeZone ); 
You'll need to work out the system ID for the Malta time zone, but you can do that easily by running this code locally:.
Console.WriteLine(TimeZoneInfo.Local.Id); 

. Judging by your comments, this bit will be irrelevant, but just for others.... If you're not using .NET 3.5, you'll need to work out the daylight savings yourself.

To be honest, the easiest way to do that is going to be a simple lookup table.

Work out the DST changes for the next few years, then write a simple method to return the offset at a particular UTC time with that list hardcoded.

You might just want a sorted List<DateTime> with the known changes in, and alternate between 1 and 2 hours until your date is after the last change:.
// Be very careful when building this list, and make sure they're UTC times! private static readonly IEnumerable<DateTime> DstChanges = ...;  static DateTime ConvertToLocalTime(DateTime utc) {     int hours = 1; // Or 2, depending on the first entry in your list     foreach (DateTime dstChange in DstChanges)     {         if (utc < dstChange)         {             return DateTime.SpecifyKind(utc.AddHours(hours), DateTimeKind.Local);         }         hours = 3 - hours; // Alternate between 1 and 2     }     throw new ArgumentOutOfRangeException("I don't have enough DST data!"); } 


3:


I don't think that you can set a property in your code that will make DateTime.Now return anything else than the current time of the computer in which the code executes.

If you want to have a way of always getting another time, you will probably need to wrap in another function.

You can can do the round-trip over UTC and add the desired offset:.
private static DateTime GetMyTime() {     return DateTime.UtcNow.AddHours(1); } 
(code sample updated after Luke's comment on the inner workings of DateTime.Now).



88 out of 100 based on 58 user ratings 958 reviews

*