0

Im working in ASP.NET MVC web project where admin can setup date time (Start Datetime/End Datetime) and timezone to display certain messages. There are other users who can either be in same timezone (as message setup) or could be in different timezone. Now the task is to display admin setup messages when the users date time matches with the one setup by admin.

What i haven't been able to figure out is what is the proper way of doing the compare with date time with different timezone including the day time saving ? Any help will be appreciated.

Update:

//to display date time picker

@(Html.Kendo().DateTimePicker()
        .Name("datetimepicker")
        .HtmlAttributes(new { style = "width:100%;" })
)

//to display timezone on admin page

var allTimeZones = TimeZoneInfo.GetSystemTimeZones();

List<SelectListItem> timeZoneLists = allTimeZones.Select(timeZone => new SelectListItem {Value = timeZone.Id, Text = timeZone.DisplayName}).ToList();
ViewBag.TimeZoneLists = timeZoneLists;

Thanks.

sanjeev40084
  • 9,227
  • 18
  • 67
  • 99
  • 1
    By always using UTC and only displaying in local time – Sami Kuhmonen Sep 23 '16 at 19:13
  • do you mean i should get time from both user and initially setup time from admin, convert them to UTC time and compare?? – sanjeev40084 Sep 23 '16 at 19:18
  • What *specifically* are you setting when you set a time zone? Show us some examples please. – Matt Johnson-Pint Sep 23 '16 at 20:50
  • the timezone will be the dropdown of timezone, so admin can choose any timezone. https://postimg.org/image/uwqiqtfsr/ – sanjeev40084 Sep 23 '16 at 21:01
  • Ok, but please give us some sample code of what you're working with. Do you have a `DateTime` and a `TimeZoneInfo` on both sides? Are you using `DateTimeOffset`? Maybe you've hardcoded a list or something else. It's unclear what your implementation is, so it's unclear how to answer. Please read from the help center: [How to create a Minimal, Complete, and Verifiable example](http://stackoverflow.com/help/mcve) and [How do I ask a good question?](http://stackoverflow.com/help/how-to-ask) – Matt Johnson-Pint Sep 23 '16 at 21:05
  • in other to get list of timezone, yes i am using TimeZoneInfo. For displaying date time (one from the screenshot) i am using .net DateTime field in Kendo UI datetimepicker. (update original post with some code) – sanjeev40084 Sep 23 '16 at 21:14

1 Answers1

0
// your input datetime and timezone
DateTime dt1 = ...
DateTime dt2 = ...
TimeZoneInfo tz1 = TimeZoneInfo.FindSystemTimeZoneById(...);
TimeZoneInfo tz2 = TimeZoneInfo.FindSystemTimeZoneById(...);

// Normalize each input to UTC
DateTime utc1 = TimeZoneInfo.ConvertTimeToUtc(dt1, tz1);
DateTime utc2 = TimeZoneInfo.ConvertTimeToUtc(dt2, tz2);

// compare
if (utc1 > utc2)
{
   // whatever
}

Before you do the conversion, be sure that the .Kind property of the dt1 and dt2 values are both DateTimeKind.Unspecified. If they were populated with Local or Utc kind, then this won't behave as expected. (This is usually controlled at time of deserialization.)

Matt Johnson-Pint
  • 230,703
  • 74
  • 448
  • 575
  • so for the first date time, tiemzone settings i can retrieve the information since it is setup by admin in the web page and it is saved in database. However for the user side, how do i retrieve their timezone from their browser? – sanjeev40084 Sep 26 '16 at 15:06
  • That's a completely different question, and [has been answered before](http://stackoverflow.com/a/18252251/634824). You might also read [this](http://stackoverflow.com/a/16526897/634824), [this](http://stackoverflow.com/a/18178299/634824), and [this](http://stackoverflow.com/a/16351529/634824). – Matt Johnson-Pint Sep 26 '16 at 16:34