c# - Handling DATETIME and DATE for Start and End dates -


i have table holds information when person on leave. 2 key fields are:

**sql

startdate date not null, enddate date null 

within .net code, these stored datetime types.

so, if user has holiday 13th of august, until 13th of august, thats 1 days leave. dates should inclusive, start being @ 0:00 (midnight), , enddate being 23:59.59 basically.

so, depending on if it's start, or end, they're treated differently.

my system works whole days. i.e. date values. don't use time @ all, forced because of datetime type in .net. they're set midnight when reading database.

the code focusing on, isn't working, this:

public string availabletoday     {                 {             if (nonavailibility != null)             {                 var = nonavailibility.firstordefault(t => datetime.utcnow <= t.enddate && datetime.utcnow >= t.startdate);                 if (i != null) return i.nonavailibilitytype ;                 return "";             }             return "";         }     } 

the point of code, return reason person not being available today. if person available, should return empty string.

the problem is, user testing with, has leave fromthe 12th of august, 13th of august.

that should 2 full days. because datetime.utcnow returns current date time, , we're 13th now... code saying not on leave today.

a fix may add 1 day enddate value? so, instead of 12th of aug 13th of aug, check 12th of aug, 14th midnight. seems hacky, may solution. or, truncate datetime.utcnow set midnight, , change check to:

t => datetime.utcnow <= t.enddate && settomidnight(datetime.utcnow) >= t.startdate 

(or, instead of using 'settomidnight....', use datetime.utcnow.date)

is there better way handle solution, or need 1 of above possible solutions?

(note, timezones , being handled in question) 10th

try datetime.utcnow.date

public string availabletoday {         {         if (nonavailibility != null)         {             var = nonavailibility.firstordefault(t => datetime.utcnow.date <= t.enddate && datetime.utcnow.date >= t.startdate);             if (i != null) return i.nonavailibilitytype ;             return "";         }         return "";     } } 

i hope help.


Comments

Popular posts from this blog

ios - UICollectionView Self Sizing Cells with Auto Layout -

node.js - ldapjs - write after end error -

DOM Manipulation in Wordpress (and elsewhere) using php -