How to compare/calculate time values in vb.net? -


so have datetimepicker formatted pick/show time values. if pick time before specific time, say, 6 pm, label should "undertime", otherwise should "overtime". want find timespan difference between picked time , 6 pm.

i've tried these codes, didn't work wanted:

if tp.value > #6:00:00 pm#  label1.text = "overtime: " & (tp.value - #6:00:00 pm#).tostring  else  label1.text = "undertime: " & (#6:00:00 pm# - tp.value).tostring   end if  

use timespan instead of datetime. problem date has time part of 18 correct, it's date part's year 0 (since date literal doesn't contain date-part it's initialized default date) lower datetimepicker's date.

so use timespan , compare datetime.timeofday timespan:

dim time = timespan.fromhours(18) if tp.value.timeofday > time     label1.text = "overtime: " & (tp.value.timeofday - time).tostring else     label1.text = "undertime: " & (tp.value.timeofday - time).tostring end if 

a timespan has no am/pm-designation since it's 24h based. if want format hh:mm-format can use timespan.tostring:

dim diff timespan = tp.value.timeofday - time dim msg = string.format("{0}: {1}",    if(diff > timespan.zero, "overtime", "undertime"),    diff.tostring("hh\:mm")) label1.text = msg 

this replaces of code above.


Comments

Popular posts from this blog

Android layout hidden on keyboard show -

google app engine - 403 Forbidden POST - Flask WTForms -

c - Why would PK11_GenerateRandom() return an error -8023? -