Showing posts with label DateTime. Show all posts
Showing posts with label DateTime. Show all posts

Monday, March 28, 2016

How to test if DateTime is null

Pretty straightforward you need to remember that the .Net stack gives any time date variable a min value by default.  You just need to test for the presence of that min value.


if(test.lastupdate == DateTime.MinValue)
{
//your condition
}

Wednesday, February 17, 2016

C# Argument Out of Range DateTime

Problem:

Had a customer contact about 3rd party solution running on our IIS server.  Customer support for software was expired and they were desperate for help.  I looked at the issue which was when the web page (asp.net) loaded user received an argument out of range error.  The page had been working previously.  The error listed the issue as no date time supported for hours, minutes, seconds constructor error.

Solution:

I took a look at the code behind and found the following constructor

var endTime = new DateTime(today.Year, today.Month, today.Day, 8 + course.LengthInHours, 0, 0).ToLocalTime(timeZone);

The declaration looks syntactically correct.  The error message made me key in on the last 3 fields, hours, minutes and seconds.  I typically don't go to that level of detail when using the DateTime namespace.  But this developer had done it so I took all a look at the MSDN site for the DateTime constructor (https://msdn.microsoft.com/en-us/library/272ba130%28v=vs.110%29.aspx)

It became pretty obvious where the issue was the hour declaration was a calculated time 8 plus some value stored in a database.  I ran the test and sure enough the error condition had an hour count of 32 (24+8).   This definitely was the issue as API says:

hour
Type: System.Int32 - The hours (0 through 23).
The error was due to the value in the object being larger than 24.  The developer had assumed that the value would always be a number under 24.  The new solution which fixes the error uses the AddHours method vice trying to initialize the DateTime in the constructor.

 var tempEndTime = DateTime.Today;
 //Default is 12 AM each day.  Add 8 hours to get to the start of the workday 8 AM.
 tempEndTime = tempEndTime.AddHours(8);
 //Now add the length of the class to estimate end time of class
 if(course.LengthInHours>0)
     tempEndTime = tempEndTime.AddHours(course.LengthInHours);
 var endTime = tempEndTime.ToLocalTime(timeZone);


Wednesday, January 13, 2016

DateTime.UtcNow vs DateTime.ToUniversalTime

Problem

I was updating an algorithm on a logging solution.  The time stamps were off due to the server sitting in Arizona and the process being started in Europe.  The issue was how to get the time displayed as true UTC time.

Solution

Initially I was playing with UtcNow in the namespace.  However, this will give you the local time of the server in UTC.  That is not what we were looking for!  We needed to use ToUniversalTime which gives the true time in UTC (Zulu). 

Source

  1. https://msdn.microsoft.com/en-us/library/system.datetime.utcnow.aspx
  2. http://stackoverflow.com/questions/10884549/datetime-conversion-to-filetimeutc