Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Getting number of days from date 1

Status
Not open for further replies.

llmclaughlin

Programmer
Aug 20, 2004
140
0
0
US
Using the below code I'm coming up one day short. From todays date 8/20/2011 is 54 days, the below gived me 53.
Any suggestions?

Dim sNOW As DateTime = Now.ToShortDateString
Dim sDATE As DateTime = CType("08/20/2011", DateTime).ToShortDateString
Dim tSPAN As TimeSpan = sNOW.Subtract(sDATE.ToShortDateString)
Dim intDAYS As Int16 = tSPAN.Days
Dim strMSG As String = "August 20th 2011 ~ Number of days to go ~ " & Math.Abs(intDAYS)


Louie
 
I would guess it's because you are subtracting a date from a datetime. the actual value is 53 days N hours N minutes ...

also it doesn't make sense why you could convert a datetime structure to string to preform math calculations.

try this instead
Code:
var today = DateTime.Today;
var february8th2011 = new DateTime(2011, 2, 8);
var days = Math.Abs(today.Subtract(february8th2011).TotalDays);

Jason Meckley
Programmer

faq855-7190
faq732-7259
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top