I know there are bunch of solutions on converting timezone to timezone, but what I'd like to know whether we can get eastern time without making conversion from our local time.
-
Isn't getting a value from another value a 'conversion' by default? Why not just add/subtract hours from your local time? – Joonas Koski Dec 11 '14 at 05:03
-
1The most direct way would probably be using `TimeZoneInfo.ConvertTime(DateTime, TimeZoneInfo, TimeZoneInfo)`. – Mike Zboray Dec 11 '14 at 05:23
-
And where would the second TimeZoneInfo come from assuming the first is the systems current TimeZoneInfo which is even more incorrect if that info changes at the time of the call.Instead consider DateTimeOffset or using UTC as I indicated in my answer. – Jay Dec 12 '14 at 01:10
2 Answers
Well yes and no, if you represent using GMT always then you don't need to convert until you need to show locality.
Time is always in GMT and services should run under the GMT Timezone because of this reason among others.
Receiving and Storing a time in anything other than GMT / UTC requires conversions.
Please note the Timezone of the server should also be set to GMT for things to work as I indicated.
When you set things up like this it thus becomes much easier to reference and compare times and dates in different calendar date formats also.
This is the reason why DateTime offset made its way into the framework and sql server. if not careful when the Timezone on a server changes so does all stored and loaded dates in local format.
A date comparison is a date comparison. DateTime just wraps a memory structure which is defined in ticks and makes methods to access commonly used parts of the memory e.g. day or year or Time or TimeOfDay etc.
Furthermore conversion is only possible if you know both the source and destination offsets and then the calculation is always as given is given by -1 * (sourceOffset - destOffset)
Where the part in parenthesis represents the time zone difference.

- 3,276
- 1
- 28
- 38
-
That's the easy part. The hard part is knowing whether the offset from UTC is -5 or -4 on the date in question. – Jon Hanna Dec 12 '14 at 11:04
-
I'm not really sure if this is the right answer, but as of now I used the offset as a workaround. Hopefully it will not be a problem with DST. I thank you guys for your answers. – user2964556 Dec 12 '14 at 11:51
-
-
Furthermore without a calendar system dst is again not observed correctly e.g if dst dates and times offsets change – Jay Dec 12 '14 at 14:54
Where the DateTime
you want to convert is in UTC and called theDate
DateTime eastern = TimeZoneInfo
.ConvertTimeFromUtc(
theDate,
TimeZoneInfo.FindSystemTimeZoneById("Eastern Standard Time"))
If theDate
has a Kind
of Local
this will throw, only accepting Kind
values of Utc
or Unspecified
. Of course, moving from local to UTC is easy and we could even check and do this conversion when required, but since you say you want this conversion "without making conversion from our local time" I'm assuming you have your times in UTC and hence having the exception would be better as it would warn you that times that should be UTC are being treat as local and a bug could pop up elsewhere.
This will use Eastern Daylight Time when theDate
is a time when EDT is in effect, as that is the normal rule for EST. To use EST even when it is the summer you can create your own TimeZoneInfo:
TimeZoneInfo
.ConvertTimeFromUtc(
theDate,
TimeZoneInfo.CreateCustomTimeZone(
"Eastern Standard Time No Daylight Savings",
new TimeSpan(-5, 0, 0),
"Eastern Standard Time No Daylight Savings",
"Eastern Standard Time No Daylight Savings"))

- 110,372
- 10
- 146
- 251
-
This is still venerable to dst change on the calendar system if the year in question the dates or times for dst changes.... – Jay Dec 12 '14 at 14:52
-
-
Isn't that outside the the scope of the question? There are possibly other situations e.g. dates in other calendar system where this assumes only the timezone changes but that could be outside of the scope of the question also.... what if the source or destination time zone is custom ... etc. – Jay Dec 12 '14 at 16:25
-
@Jay the question is "I'd like to know whether we can get eastern time without making conversion from our local time." "Eastern Time" (clarified by a comment on a deleted post as meaning US Eastern Time) means "5 hours behind UTC except from 07:00UTC of the second Sunday in March until 06:00UTC of the first Sunday of November when it is 4 hours behind UTC). That's the question the answer above answers. You're the one who widened the scope to include potential changes to the definition of "Eastern Time". – Jon Hanna Dec 12 '14 at 17:21
-
Well that's an opinion. Eastern time is not always the same especially when different years have different dst offsets and dates so what exactly is your point again? How is that widening the question and not encompassing it entirely? The question didn't state if dst was required in the calculation and my answer skips having to think about that. That's engineering. – Jay Dec 12 '14 at 17:33
-
If I really wanted to widen the question I would have invalidated your response with something like what if the person is in an alternate universe, then also dst could be different or non existent but again.... it could be a viable part of the equation in such a case ;-) – Jay Dec 12 '14 at 17:38
-
@Jay The question did state that DST was required; it said "Eastern Time". Eastern Time is UTC-7 with Daylight Savings Time observed. If it doesn't adjust for DST, then it isn't Eastern Time. – Jon Hanna Dec 12 '14 at 17:43
-
And you knew this was specifically not eastern standard time how? The op may have or may have not understood that distinction and hence also what if the format in your string doesn't yield a valid TimeZoneInfo? P.s. how's spooky hash coming? – Jay Dec 12 '14 at 18:01