DateDiff Question

Results 1 to 2 of 2

Thread: DateDiff Question

  1. #1
    newbiegal Guest

    Default DateDiff Question

    Hi Guys,<BR>Can you use Date Diff to tell the difference between now and a time date stamp in a SQL server? I need to get the hours between now and a timestamp pulled from the database. How would I do this?<BR>Thanks in advance!<BR>Julie

  2. #2
    LeeAnnie Guest

    Default Function to get the interval between two dates

    Here you go... written by yours truly ;o) HTH<BR><BR><BR>&#039; This function will return the interval, as a string, between two dates in the following format:<BR>&#039; "(-) ..D day(s) HH:MM:SS" where "day" will be pluralized if D is not equal to 1 and HH is based on a <BR>&#039; 24 hour period. The value will be negated (-) if date1 is greater than date2. The periods denote<BR>&#039; spaces; the day is put into a three character string where spaces will fill the unused portion so that<BR>&#039; it will be sorted correctly if using Quicksort.<BR>&#039; Written by LeeAnn Vermaak on December 11, 2000<BR>function getInterval(date1, date2)<BR> temp = ""<BR> negate = false<BR> seconds = 0<BR> minutes = 0<BR> hours = 0<BR> days = 0<BR> if not (isDate(date1) and isDate(date2)) then<BR> getInterval = "Cannot Calculate"<BR> else<BR> if cDate(date1) = cDate(date2) then <BR> getInterval = "0 days 00:00:00"<BR> elseif cDate(date1) &#062; cDate(date2) then &#039; dates need to be reversed and negated<BR> temp = date1<BR> date1 = date2<BR> date2 = temp<BR> negate = true<BR> end if<BR> &#039;seconds<BR> if datePart("s",date1) &#062; datePart("s",date2) then<BR> seconds = 60 - datePart("s",date1) + datePart("s",date2)<BR> else<BR> seconds = datePart("s",date2) - datePart("s",date1)<BR> end if<BR> date1 = dateAdd("s",seconds,date1)<BR> if cDate(date1) &#060; cDate(date2) then<BR> &#039;minutes<BR> if datePart("n",date1) &#062; datePart("n",date2) then<BR> minutes = 60 - datePart("n",date1) + datePart("n",date2)<BR> else<BR> minutes = datePart("n",date2) - datePart("n",date1)<BR> end if<BR> date1 = dateAdd("n",minutes,date1)<BR> if cDate(date1) &#060; cDate(date2) then <BR> &#039;hours<BR> if hour(date1) &#062; hour(date2) then<BR> hours = 24 - hour(date1) + hour(date2)<BR> else<BR> hours = hour(date2) - hour(date1) <BR> end if<BR> date1 = dateAdd("h",hours,date1)<BR> &#039;days<BR> days = dateDiff("d",date1,date2)<BR> else<BR> days = 0<BR> hours = 0<BR> end if<BR> else<BR> minutes = 0<BR> hours = 0<BR> days = 0<BR> end if<BR> end if<BR> &#039;build interval string <BR> interval = string(3-len(cStr(days))," ")&days<BR> if cStr(days) = "1" then<BR> interval = interval + " day "<BR> else<BR> interval = interval + " days "<BR> end if<BR> interval = interval + string(2-len(cstr(hours)),"0")&hours&":"<BR> interval = interval + string(2-len(cstr(minutes)),"0")&minutes&":"<BR> interval = interval + string(2-len(cstr(seconds)),"0")&seconds<BR> if negate then<BR> interval = "- "&interval<BR> end if<BR> getInterval = interval<BR>end function

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts