Showing posts with label date. Show all posts
Showing posts with label date. Show all posts

Monday, March 26, 2012

Strange DateDiff result

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

Strange Date Problem

Hi all,

i am using visual studio 2005 and sql server 2005, i am developing a web application using visual basic 2005. i also let you know that i am in UK.

i am having a strange problem. i have got a form from whcih i need to insert the date values into the data base my querystring is as follows.

StrSql = "insert into tasks (TaskName,Sdate,PropEdate,InitID,TaskManager) values (" & _
"'" & Me.TxtTaskName.Text & "'," & _
"'" & Me.TxtSDate.Text & "'," & _
"'" & Me.TxtEdate.Text & "'," & _
Session("InitID") & "," & _
Me.DropDownList1.SelectedValue & _
")"

which is working fine except that it is storing date in different format that is mm/dd/yyyy.

on my other form i again want to insert the date value wich is not working.
my querystring is

StrSql = "insert into tasklog (taskid,currentstatus,updatedon,comments) values (" & _
Me.GridView1.SelectedValue & "," & _
Me.TxtRecentpos.Text & "," & _
"'" & Me.TxtDate.Text & "'," & _
"'" & Me.TxtComments.Text & "'" & _
")"

when i try to run this query it gives me the following error message:

The conversion of char data type to smalldatetime data type resulted in an out-of-range smalldatetime value..

i have checked in the sqlserver and both the tables have got the smalldatetime datatype for my fields. it's really scaring for me that it is working on one place and not working on other.
any help would be highly appretiated.
thanksNo matter what the settings are in your code/computer/database, when putting date values into SQL statements, you cannot use the UK format (dd/mm/yyyy).

You must use either the US format (mm/dd/yyyy), or better a format which cannot be mis-interpreted (like yyyy/mm/dd).

If you format the values in this style, you will probably find that the code works perfectly.
Hi si,

i have tried both us format and the other format you told me. but sitll giving the same erroe message.

regards
Faizee
You must use either the US format (mm/dd/yyyy), or better a format which cannot be mis-interpreted (like yyyy/mm/dd).

If you format the values in this style, you will probably find that the code works perfectly.[/QUOTE]

Hi Si sorry i have tried (mm-dd-yyyy) and (yyyy-mm-dd) both are working fine. is it not possible that user enter the date in UK format ( i am using vb 2005) and then i format this to us format or other format to save in the SQL Server so that the users are not confused?

regards
Of course... you just have to convert it before using it in an SQL string.

I haven't used .Net enough to suggest the code you would use to convert it, but can recommend that you use a special date control rather than a textbox - as this is always clear to the user, and to your code too (no matter what settings the PC has).
The conversion of char data type to smalldatetime data type resulted in an out-of-range smalldatetime value..

This can be caused by trying to put a date less than 1/1/1900 or larger than 6/6/2079

Also check out using parameters with your SQL strings, it prevents sql injection as well has handles special characters.
Thanks Si and Bill,

i am using RJS Calendar control now and it's working fine.

thanks a lot for your help

Faizee

Tuesday, March 13, 2012

strange problem

I have one very strange problem. I have asp:textbox control for entering date. I use asp:calendar control to fill the date. I open the asp:calendar control in new web form with javascript. User selects the date and fill the asp:textbox with that date. It works fine. But when I click the button which find the total; the value in that asp:textbox control which has date does not stay. All other control can maintain their values. Please help me.Is your textbox disabled. ASP.net has a problem with getting values from disabled controls on postback. My advice use javascript to re-enable the textbox on submit and turn it off after your done then you will see your values.