Reputation: 4199
I coded this snippet to log the users IP and time on my website. It works but something is wrong with the time:
public static void UserLogin(string iPaddress, string uname)
{
DateTime dt = TimeZoneInfo.ConvertTimeToUtc(DateTime.Now);
string cet= dt.AddHours(1).ToString("F", new CultureInfo("en-US"));
.....
}
The website is on a server somewhere in UK and to adjust the login time to CET without going too sophisticated, I simply toughth adding the hours difference using (AddHours) but for some reason, and I do not understand why, whatsoever number I put in there "AddHours(1)" will never get added and moreover, right now that is 13:55 at my location in Italy, the time recorder by the method is 1:55 am that is 12 hours behind even if there is no hours added "AddHours(0)". Some help to understand what is going on in this method will be appreciated. Thanks.
Upvotes: 1
Views: 62
Reputation: 16878
You can convert time between time zones in more controlled way, for example:
DateTime nowutc = DateTime.UtcNow;
var cet = TimeZoneInfo.FindSystemTimeZoneById("Central European Standard Time");
var nowcet = TimeZoneInfo.ConvertTimeFromUtc(nowutc, cet);
Upvotes: 1