Hi
Can anyone tell me why I get a date difference of 0 for the following code? I was expecting 1.
The code:
strDateDif = Ctype(DateDiff("d", dtItem, dtToday), String)
Response.Write("DateDif between " & dtItem & " and " & dtToday & " = " & strDateDif & "<br />")
The result:
DateDif between 27/02/2006 09:42:43 and 28/02/2006 08:57:16 = 0
Many thanks
Shaun
Hi Shaun,
It is less then a day difference
Ah, so a 'day' ago is at least 24 hrs ago rather than simply yesterday? Makes sense.
If so, is there a way to distinguish between today and yesterday?
My script retrieves website hits for each day that has received at least one hit in the last 7 days. This means the SQL may not pass data for all 7 days. Only those days that have at least one hit.
This data is shown on a graph. To create each bar of the graph, I set the height of one of the images; imgDay0, imgDay1, imgDay2, imgDay3, imgDay4, imgDay5, imgDay6.
I have the HitDate and HitNumber as a par in a HashTable.
For each item in the HashTable, I need to set the height of the bar image on the graph corresponding to the date.
To do this, I use the DateDiff to work out the name of the image we are targeting.
If the DateDiff is 0, we're targeting img0, if the DateDiff is 1, we target img1 etc. My code is below:
For Each objItem in statsHashTable
dtItem = objItem.Key
intStat = objItem.Value
strDateDif = Ctype(DateDiff("d", dtItem, Now()), String)
Response.Write("DateDif between " & dtItem & " and " & dtToday & " = " & strDateDif & "<br />")
imgDayName = "imgDay" & strDateDif
imgDay = FindControl(imgDayName)
intPercentage = (intStat/intHighNumber) * 100
intBarHeight = Math.Round((intGraphHeight/100) * intPercentage)
imgDay.Height = Unit.Pixel(intBarHeight)
imgDay.ToolTip = CType(intStat, String)
Next
Is there another way to ensure that if the date we are using is 'yesterday', we get a DateDiff of 1? Should I be using something other than "d" in my DateDiff?
Many thanks
Shaun
Thetime is also taken into consideration in the DateDiff function. as you can see the the difference isless that one day when the time is also considered. So it is returning as 0.
If needed you can format the date to dd/mm/yyyy format without the time to get the desired result
Regards
Hi,
You can compare dtItem.Day with Now().Day instead of using DateDiff.
Brilliant. Yes, both of these options work.
I have opted for the first. I simply format the dates like so:
myDate.ToString("D") before using DateDiff. This strips out the time and leaves just the date, which then gives me the desired answer.
Many thanks for your help.
Shaun
0 comments:
Post a Comment