Question: I'm storing the user's timezone as a decimal in the session. For instance, if the user is in the EST timezone, I'd have 

```
UserTimeZone = -5.00;

```

The data in the database is stored in UTC, so I want to calculate the beginning and the end of the day for that user so that when the user wants his data for a specific day, the records are timezone adjusted.

This is what I'm doing:

```
DateTime StartDate =  DateTime.Now.ToUniversalTime();

StartDate = StartDate.AddHours((double)UserTimeZone);
StartDate = StartDate.Date;
StartDate = StartDate.AddHours((double)UserTimeZone);

DateTime EndDate = StartDate.AddHours(24);

```

The problem I'm having is that this doesn't account for daylight saving time so even thought EST time is 5 hours behind UTC, for the moment it's actually 4 hours behind UTC because of the daylight saving shift.

What are some of your suggestions? Thanks.

Answer: 
To make such calculations you will need to use the `TimeZoneInfo` and `DateTimeOffset` classes.

First of all, we need to get a `TimeZoneInfo` instance for both the local time and the user's local time:

```
var localTimezone = TimeZoneInfo.Local;
var userTimezone = TimeZoneInfo.FindSystemTimeZoneById("Eastern Standard Time");

```

The caveat here is that you need to know the id of the user's local timezone (the offset that you currently have is not going to be enough). You can get a list of all `TimeZoneInfo` instances that the system recognizes with [`TimeZoneInfo.GetSystemTimeZones`](http://msdn.microsoft.com/en-us/library/system.timezoneinfo.getsystemtimezones.aspx), and then you need a way of matching the user-provided timezone with one of those.

For this example, I have hardcoded EST.

Then you need to get a `DateTimeOffset` instance for today's midnight (start of day) in your local timezone:

```
var todayDate = DateTime.Today;
var todayLocal = new DateTimeOffset(todayDate,
                                    localTimezone.GetUtcOffset(todayDate));

```

Given this, you can calculate the `DateTimeOffset` instance that represents the midnight of "today" (based on your local time) in the user's timezone. **Be aware that, depending on timezones, this may actually be in the future for the user's timezone!**

```
var todayUser = TimeZoneInfo.ConvertTime(todayLocal, userTimezone);

```

And finally, you can create timestamps for both dates like this:

```
var epochStart = DateTime.Parse("01/01/1970 00:00:00");
var todayLocalTs = (todayLocal.Ticks - epochStart.Ticks)/TimeSpan.TicksPerSecond;
var todayUserTs = (todayUser.Ticks - epochStart.Ticks) / TimeSpan.TicksPerSecond;

```
I would recommend adding a timezone to the users settings and storing that. Timezones have different times depending on the time of year. you can provide a list of time zones for the user to choose from by using the [TimeZoneInfo.GetSystemTimeZones](http://msdn.microsoft.com/en-us/library/system.timezoneinfo.getsystemtimezones.aspx) method. You can store any date as UTC and convert it (with the [TimeZoneInfo.ConvertTime](http://msdn.microsoft.com/en-us/library/bb382770.aspx) method) to the users time when displaying, and convert it back to UTC when saving it. This will allow the user to change their timezone at any time without causing problems. If you follow this format you should not run into any problems. 

Be forewarned, if you do not store the dates as UTC and convert as suggested above you may run into problems. In some time zones certain times do not exist on certain days when changing from daylight savings time to standard time. The TimeZoneInfo class does not play nice with these non-existent times.