convert local time to another country time
Hi,
My web server is not my country, so server's time and my country time is
different.
So code runs on server,
for example when I run DateTime.Now() function
it returns server time but I want to get my local time.
Is there a way to do that?
Hope your's reply.
Thanks
Re: convert local time to another country time
Use ToLocalTime function of datetime class.
DateTime dt = DateTime.Now.ToLocalTime();
Re: convert local time to another country time
Not sure what that does, but you should use TimeZoneInfo. You'd have to create a TimeZoneInfo object, telling it which time zone you want (name, I think) and you can then use its method to convert another time to your time zone time.
Re: convert local time to another country time
From Omer van Kloeten's .NET Zen:
The Death of System.DateTime?
It’s been a few months since I started getting acquainted with the System.DateTimeOffset type and I can honestly say I don’t see any reason to use System.DateTime anymore.
I’ve even gone as far as ask whether anyone knew when I would rather use DateTime over DateTimeOffset. The responses I got were along the lines of ‘backwards compatibility’ or ‘when you need an abstract time’. My recommendation is that if you haven’t yet looked at the type, go do it now and after that, start using it.
So what is this DateTimeOffset? When representing a date/time, especially in an internationally-faced system, you have to include a time-zone. DateTime did a very poor job handling time-zones, like being insensitive to changes. DateTimeOffset is the exact same thing as DateTime, only it takes heed of time-zones. For instance, comparing two DateTimeOffsets with the same UTC time in different time-zones will result in equality.
Moreover, DateTime also had only three modes: Local, UTC and Unspecified, whereas DateTimeOffset has an Offset property, allowing you to create dates in any time-zone you like.