473,396 Members | 2,039 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Trying to get Date Diff or Timespan a simple way.

6
Hello,

Please keep in mind when you read this post that I am a total noob when it comes to coding.

Coding in ASP
On MS Server
SQL Database/Table

I know this topic has been covered many times and many ways. In fact I've tried to use one version or another of 20 examples with no success. All I am trying to do is determine the timespan between the date a sql record was submitted and the current date.

Most of the examples I have tried to massage into my asp page have stopped the page from loading. The example I have included below does let my page load and shows my date variables and allows the subtraction to work. However, I end up with a number with a lot of decimal places after it.

I have included my code below and the output from my code. I realize it is very simple coding but I was trying to avoid having a big block of code that I don't understand. As silly as it may seem to you pros I have spent an unbelievable amount of time trying to make this work so any assistance would be greatly appreciated!!!! I have no hair left to pull out!

I'm trying to get to a point where the output displays the amount of days(if over 24 hrs) and the amount of hours.

Do I need to convert that number somehow or am I doing something completely wrong?

Expand|Select|Wrap|Line Numbers
  1. Dim StartTime
  2. Dim EndTime
  3.  
  4. StartTime = rs("LastUpdated") 'record in sql table stored as datetime
  5. EndTime = (Now) 'current date
  6.  
  7. response.write starttime & "<br>"
  8. response.write endtime & "<br><br>"
  9.  
  10. Dim diff1
  11. diff1 = EndTime - StartTime
  12.  
  13. Response.Write diff1 
  14.  
[Output]
3/12/2008 11:11:53 AM
3/13/2008 12:15:11 PM

1.04395833332819
[/Output]

Please help!!
Mar 13 '08 #1
5 3388
jeffstl
432 Expert 256MB
Sounds like you want to use the datediff function of VB

Explore this link to find out more. Yours should look something like this when your done, if you want to use hours as your final output, otherwise just replace the h with m for minutes, etc. Read the link to see the whole function in use.

diff1 = DateDiff(h,EndTime,StartTime)

http://www.w3schools.com/vbscript/func_datediff.asp
Mar 13 '08 #2
Pauley
6
Sounds like you want to use the datediff function of VB

Explore this link to find out more. Yours should look something like this when your done, if you want to use hours as your final output, otherwise just replace the h with m for minutes, etc. Read the link to see the whole function in use.

diff1 = DateDiff(h,EndTime,StartTime)

http://www.w3schools.com/vbscript/func_datediff.asp
Hi Jeff,

Thanks so much for the reply! I attempted to use the examples on w3schools when I began this project but had no luck. However it did help me to understand a lot. I rely on that site a lot. I was able to use a variation of the line of code you gave me and got the page to produce the hours since a record had been submitted. The line of code is below.

Expand|Select|Wrap|Line Numbers
  1. StartTime = rs("date") 'datetime field in the sql table
  2. EndTime = (Now) 'current date and time
  3.  
  4. response.write(DateDiff("h",EndTime,StartTime) & "<br>")
  5.  
[Output]
-21
[/Output]

However I still had some issues to deal with.

1. The value that was returned was a negative number. Im not sure why since the EndTime is always higher than the StartTime. I reversed the EndTime and StartTime and it returns a positive number.

I'm not sure it thats the correct way to resolve that or not?

2. I have a datetime format field in the sql table called date where I submit EndTime. Which is just EndTime = (Now) For some reason when I submit a record the date always gets submitted as the current date but the time always gets entered as 12:00:00 AM.

Any idea why it won't record the time from EndTime = (Now)?

Any assistance you can offer would be greatly appreciated!
Mar 18 '08 #3
jeffstl
432 Expert 256MB
Hi Jeff,

Thanks so much for the reply! I attempted to use the examples on w3schools when I began this project but had no luck. However it did help me to understand a lot. I rely on that site a lot. I was able to use a variation of the line of code you gave me and got the page to produce the hours since a record had been submitted. The line of code is below.

Expand|Select|Wrap|Line Numbers
  1. StartTime = rs("date") 'datetime field in the sql table
  2. EndTime = (Now) 'current date and time
  3.  
  4. response.write(DateDiff("h",EndTime,StartTime) & "<br>")
  5.  
[Output]
-21
[/Output]

However I still had some issues to deal with.

1. The value that was returned was a negative number. Im not sure why since the EndTime is always higher than the StartTime. I reversed the EndTime and StartTime and it returns a positive number.

I'm not sure it thats the correct way to resolve that or not?

2. I have a datetime format field in the sql table called date where I submit EndTime. Which is just EndTime = (Now) For some reason when I submit a record the date always gets submitted as the current date but the time always gets entered as 12:00:00 AM.

Any idea why it won't record the time from EndTime = (Now)?

Any assistance you can offer would be greatly appreciated!
Q1) StartTime should be first, End Time second to get the difference you want.

Q2) To store the returned value of the Now() function in a way you want you will need to use the VB Format function

Format(Now, "MM/DD/YYYY") ---will return 03/18/2008 - However I think for ASP its called FormatDateTime() but works the same.

http://www.w3schools.com/vbscript/fu...atdatetime.asp
Mar 18 '08 #4
Pauley
6
Q1) StartTime should be first, End Time second to get the difference you want.

Q2) To store the returned value of the Now() function in a way you want you will need to use the VB Format function

Format(Now, "MM/DD/YYYY") ---will return 03/18/2008 - However I think for ASP its called FormatDateTime() but works the same.

http://www.w3schools.com/vbscript/fu...atdatetime.asp
Hi Jeff,

Still working on this thing and I've got it down to one problem. I have my date formatted as EndTime = FormatDateTime(Now(),0)

When I write out the EndTime variable to the screen before submitting it writes it as it should with both the current date and time. However, once I submit it to my datetime field in my table it still records the time portion of the date as 12:00:00 AM instead of the current time.

Displays on screen before submitting as: 3/23/2008 2:10:24 PM
Records in sql table datetime field as: 3/23/2008 12:00:00 AM

Any idea why it won't record the proper time?
Mar 23 '08 #5
Pauley
6
Hi Jeff,

Still working on this thing and I've got it down to one problem. I have my date formatted as EndTime = FormatDateTime(Now(),0)

When I write out the EndTime variable to the screen before submitting it writes it as it should with both the current date and time. However, once I submit it to my datetime field in my table it still records the time portion of the date as 12:00:00 AM instead of the current time.

Displays on screen before submitting as: 3/23/2008 2:10:24 PM
Records in sql table datetime field as: 3/23/2008 12:00:00 AM

Any idea why it won't record the proper time?
Nevermind, I got it!

Thanks for all the help!
Mar 24 '08 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

28
by: Steve | last post by:
Hi all How would I find out the average date when given a bunch of dates? For example, I want to find the average length in time from the following dates:...
5
by: A | last post by:
Hi Group, Coming from a VB background I am looking for the quickest path to do a date diff in C#. I see that C# has no date diff function so this must be more obvious than it looks. Thanks
2
by: Cyber Clone via DotNetMonster.com | last post by:
Hello All, i want to know how to date different in csharp. i can datediff in vb6. is there any simple command for date different (CSharp)? thanks Cyber Clone
29
by: james | last post by:
I have a problem that at first glance seems not that hard to figure out. But, so far, the answer has escaped me. I have an old database file that has the date(s) stored in it as number of days. An...
8
by: JFB | last post by:
Hi All, I'm trying to find if a file name that includes the date is before todays to do some procedures. In my IF statement is working is just a few dates before but not is a year before. How...
2
by: Brian Shafer | last post by:
In vb 6 i had a something like result = CLng(UserDate) - CLng(MyDate) in vb.net I get an error "Value of tyep 'Date' cannot be converted to 'Long'. How do i get around this?
6
by: rohayre | last post by:
Im a long time java developer and actually have never done anything with java scripting. I'd like to write a short simple script for calculating a date in the future based on today's date and a...
7
by: Gucci | last post by:
<?php /** * check ine year is a leap year, and return the month day array * * @param int $year **the year must bigger than zero** * @return array */ function is_leap_year($year){...
0
yasirmturk
by: yasirmturk | last post by:
Standard Date and Time Functions The essential date and time functions that every SQL Server database should have to ensure that you can easily manipulate dates and times without the need for any...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.