title: "Get a place's open and close hours and if DateTime.Now is between mark as open " works locally but not on the server after published
body: <p>I have a problem with times when debugging locally versus when published on a server.</p>
  <p>My need is to know if a store is open between 2 times such as:</p>
  <pre><code>if ((timeNow &gt;= openHour) &amp;&amp; (timeNow &lt;= closeHour))
  </code></pre>
  <p>And if it is, tell the user if the store is closed or open.</p>
  <p><strong>Edit</strong></p>
  <p><strong>My question basically:</strong>
  The Cron Job that updates the stores' state every minute queries the stores' timezones and for each store adapt its DateTime.UtcNow to that timezone, and it works when debugging locally but not when I've published the code and query the stores' state.</p>
  <p>Here's what I'm trying to do:</p>
  <p><strong>StoreHours.cs</strong></p>
  <pre><code>public class StoreHour : AuditableEntities
  {
      public StoreHour()
      {
  
      }
  
      public Guid StoreHourId { get; set; }
      public Guid DayOfTheWeekId { get; set; }
      public virtual DayOfTheWeek DayOfTheWeek { get; set; }
      public DateTime OpenHour { get; set; }
      public DateTime CloseHour { get; set; }
      public Guid PlaceId { get; set; }
      public virtual Place Place { get; set; }
  }
  </code></pre>
  <p><strong>AppDbContext.cs ( seed data )</strong></p>
  <pre><code>// Hours for the first store
  var cscOpeningHour = DateTime.ParseExact(&quot;11:30:00 AM&quot;, &quot;hh:mm:ss tt&quot;, CultureInfo.InvariantCulture);
  cscOpeningHour = DateTime.SpecifyKind(cscOpeningHour, DateTimeKind.Utc);
  var cscClosingHour = DateTime.ParseExact(&quot;10:00:00 PM&quot;, &quot;hh:mm:ss tt&quot;, CultureInfo.InvariantCulture);
  cscClosingHour = DateTime.SpecifyKind(cscClosingHour, DateTimeKind.Utc);
  
  // Hours for the second store ( have multiple because closing time is the next day ( 02:00:00 AM )
  var jOpeningHour1 = DateTime.ParseExact(&quot;12:00:00 AM&quot;, &quot;hh:mm:ss tt&quot;, CultureInfo.InvariantCulture);
  jOpeningHour1 = DateTime.SpecifyKind(jOpeningHour1, DateTimeKind.Utc);
  var jClosingHour1 = DateTime.ParseExact(&quot;02:00:00 AM&quot;, &quot;hh:mm:ss tt&quot;, CultureInfo.InvariantCulture);
  jClosingHour1 = DateTime.SpecifyKind(jClosingHour1, DateTimeKind.Utc);
  var jOpeningHour2 = DateTime.ParseExact(&quot;05:00:00 PM&quot;, &quot;hh:mm:ss tt&quot;, CultureInfo.InvariantCulture);
  jOpeningHour2 = DateTime.SpecifyKind(jOpeningHour2, DateTimeKind.Utc);
  var jClosingHour2 = DateTime.ParseExact(&quot;11:59:59 PM&quot;, &quot;hh:mm:ss tt&quot;, CultureInfo.InvariantCulture);
  jClosingHour2 = DateTime.SpecifyKind(jClosingHour2, DateTimeKind.Utc);
  
  // Seed data
  modelBuilder.Entity&lt;StoreHour&gt;().HasData(new StoreHour
          {
              StoreHourId = Guid.NewGuid(),
              DayOfTheWeekId = mondayDayOfTheWeekId,
              OpenHour = cscOpeningHour,
              CloseHour = cscClosingHour,
              PlaceId = cscPlaceId
          });
  </code></pre>
  <p><strong>UpdateDeviceCommandHandler.cs</strong></p>
  <pre><code>public async Task&lt;Unit&gt; Handle(UpdateDeviceCommand request, CancellationToken cancellationToken)
      {
          var devices = await repository.GetAllDevicesWithRelatedData();
          foreach (var device in devices)
          {
              var count = await repository.GetNumber(device.DeviceId);
  
              var thePlace = await placeRepository.GetPlaceByIdWithRelatedData(device.Place.PlaceId);
  
              var timeNow = TimeZoneInfo.ConvertTime(
                                DateTime.Now,
                                TimeZoneInfo.FindSystemTimeZoneById(thePlace.Timezone.Name));
  
              var day = DateTime.UtcNow.DayOfWeek.ToString();
  
              if (thePlace.StoreHours.Count() &gt; 7)
              {
                  // OpenHour and CloseHour are DateTime properties
                  // returns &quot;12:00:00 AM&quot;
                  var openHour1 = thePlace.StoreHours.OrderBy(d =&gt; d.OpenHour).Select(d =&gt; d.OpenHour).First().ToString(&quot;hh:mm:ss tt&quot;);
                  // returns &quot;2:00:00 AM&quot;
                  var closeHour1 = thePlace.StoreHours.OrderBy(d =&gt; d.CloseHour).Select(d =&gt; d.CloseHour).First().ToString(&quot;hh:mm:ss tt&quot;);
                  // returns &quot;05:00:00 PM&quot;
                  var openHour2 = thePlace.StoreHours.OrderByDescending(d =&gt; d.OpenHour).Select(d =&gt; d.OpenHour).First().ToString(&quot;hh:mm:ss tt&quot;);
                  // returns &quot;11:59:59 PM&quot;
                  var closeHour2 = thePlace.StoreHours.OrderByDescending(d =&gt; d.CloseHour).Select(d =&gt; d.CloseHour).First().ToString(&quot;hh:mm:ss tt&quot;);
  
                  // returns today's date and time: mm/dd/yyyy - 12:00:00 AM
                  // Which is the right time converted to the place's timezone
                  var theOpenHour1 = DateTime.Parse(openHour1);
                  theOpenHour1 = TimeZoneInfo.ConvertTime(
                                theOpenHour1,
                                TimeZoneInfo.FindSystemTimeZoneById(thePlace.Timezone.Name));
                  var theCloseHour1 = DateTime.Parse(closeHour1);
                  theCloseHour1 = TimeZoneInfo.ConvertTime(
                                theCloseHour1,
                                TimeZoneInfo.FindSystemTimeZoneById(thePlace.Timezone.Name));
                  var theOpenHour2 = DateTime.Parse(openHour2);
                  theOpenHour2 = TimeZoneInfo.ConvertTime(
                                theOpenHour2,
                                TimeZoneInfo.FindSystemTimeZoneById(thePlace.Timezone.Name));
                  var theCloseHour2 = DateTime.Parse(closeHour2);
                  theCloseHour2 = TimeZoneInfo.ConvertTime(
                                theCloseHour2,
                                TimeZoneInfo.FindSystemTimeZoneById(thePlace.Timezone.Name));
  
                  if ((timeNow &gt;= theOpenHour1) &amp;&amp; (timeNow &lt;= theCloseHour1)
                       || (timeNow &gt;= theOpenHour2) &amp;&amp; (timeNow &lt;= theCloseHour2))
                  {
                      if (count != 0)
                      {
                          device.Ratio = (double)device.Count / (double)count;
                      }
                      else
                      {
                          count += 1;
                          device.Ratio = (double)device.Count / (double)count;
                      }
  
                      if (device.Ratio == 2)
                      {
                          //
                      }
                      if (...)
                      {
                          //
                      }
  
                      repository.Update(device);
                  }
                  else
                  {
                      //
  
                      repository.Update(device);
                  }
              }
  
              else
              {
                  var openHour = thePlace.StoreHours.Select(d =&gt; d.OpenHour).First().ToString(&quot;hh:mm:ss tt&quot;);
                  var closeHour = thePlace.StoreHours.Select(d =&gt; d.CloseHour).First().ToString(&quot;hh:mm:ss tt&quot;);
  
                  DateTime dtOpen = DateTime.Parse(openHour);
                  dtOpen = TimeZoneInfo.ConvertTime(
                                dtOpen,
                                TimeZoneInfo.FindSystemTimeZoneById(thePlace.Timezone.Name));
                  DateTime dtClose = DateTime.Parse(closeHour);
                  dtClose = TimeZoneInfo.ConvertTime(
                                dtClose,
                                TimeZoneInfo.FindSystemTimeZoneById(thePlace.Timezone.Name));
  
                  if ((timeNow &gt;= dtOpen) &amp;&amp; (timeNow &lt;= dtClose))
                  {
                      if (count != 0)
                      {
                          device.Ratio = (double)device.Count/ (double)count;
                      }
                      else
                      {
                          count += 1;
                          device.Ratio = (double)device.Count/ (double)count;
                      }
  
                      if (device.Ratio == 2)
                      {
                          //
                      }
                      if (...)
                      {
                          //
                      }
  
                      repository.Update(device);
                  }
                  else
                  {
                      //
  
                      repository.Update(device);
                  }
              }
  
          await repository.SaveChanges();
  
          return Unit.Value;
  
          }
  </code></pre>
  <p>As @JonasH said I should use TimeSpan but before I do I really want to understand why it's working locally and not remotely. I'm converting the invalid date's time &quot;12:00:00 AM&quot; into a valid DateTime converted to the place's TimeZone ( right before the if statement ). When I debug it it works because the times compared are the same day and the right times, what confuses me is why not on the server, what is it that I'm not converting or not doing that makes the server not do it.</p>
  <p>I'm sure it has to do with time conversion as my computer gets the local time but I don't know how the server gets it wrong after having converted the time values in the code.</p>
  <p>Thank you so much for your help !</p>