Home » ASP » Article
Add or Subtract Hours in SQL or ASP using DateAdd
|
| Article by: | Jeff Anderson ( 1361 ) (3/17/2003) |
|
| Sponsored by: | FindMyHosting - Web Hosting Search |
| Summary: | A beginners guide to using the SQL DATEADD function to add or subtract hours. Particularly useful when setting the time displayed on the ASP page to a different time zone (eg when the server is in the US, and the site is for a UK audience). |
|
| Viewed: 49343 times |
Rating (26 votes): |
|
4 out of 5 |
|
|
|
Add or Subtract Hours in SQL or ASP using DateAdd
I had a situation with some code where I needed to display the time of a post, but the site was a UK based community. Trouble was, my server was in the US. So I needed on a regular basis to add 5 hours to the date any one made a post.
It's actually very simple to do, using the DateAdd function. Here it is for SQL server
and for Access:
You can use this for both ASP pages and for SQL code in SQL server.
Like the similar DatePart function we specify in the first part the type of time (hours, days, minutes etc) and then the number to add, then the date (in this case the current date, but it might be a selected date variable). So the above code adds 5 hours to the current time - perfect!
If you need to subtract, you still use DateAdd but use minus numbers.
Here's the full list of options
| Setting |
Description |
| yyyy |
Year |
| q |
Quarter |
| m |
Month |
| y |
Day of year |
| d |
Day |
| w |
Weekday |
| ww |
Week of year |
| h |
Hour |
| n |
Minute |
| s |
Second |
One interesting point with adding months. Let's say you have a date 31st of Jan and you add a month. It will not just say 31st Feb (which doesn't exist of course), but will pick the last day of the month, even selecting 29th of Feb if it happens to be a leap year!
|
|
View highlighted Comments
User Comments on 'Add or Subtract Hours in SQL or ASP using DateAdd'
|
Posted by :
Archive Import (Dave) at 21:58 on Saturday, March 29, 2003
|
This would not work for me.
"getdate" does not seem to be a valid comand only "date" was valid.
| |
Posted by :
Archive Import (jeff) at 04:37 on Sunday, March 30, 2003
|
dave - getdate() is for SQL server only. Silly me! I've added an example using the Access equivalent - date()
Thanks
jeff
| |
Posted by :
Archive Import (Jon Harbour) at 01:08 on Saturday, May 17, 2003
|
What about when daylight savings is switched on and your target location is in a place without daylight savings?
| |
Posted by :
Archive Import (Dan) at 07:55 on Thursday, May 22, 2003
|
Does anyone know the DB2 equivalente
for the DATEADD funtion?
| |
Posted by :
Archive Import (victor) at 05:20 on Thursday, June 05, 2003
|
This is the problem i need to solve:
I am currently using DateAdd()as in the following example:
DATEADD(s,[Create_date] + 3600,'01/01/1970') AS Create_date
You can see that I am adding 3600 secs. In summer months, I need to change this parameter to 7200 secs for clients in another time zone.
How can I do this automatically ?
Thanks,
Victor.
| |
Posted by :
Archive Import (jacob) at 01:55 on Saturday, July 19, 2003
|
i want to add hour and minute together i.e: 9 hours and 28 minutes.
| |
Posted by :
Archive Import (jacob) at 01:58 on Saturday, July 19, 2003
|
i want to add 9 hours and 28 minutes togethrt in email_date field
select dateadd(hour,9,email_date) as date1 from support_email
select dateadd(minute,28,date1) from support_email
| |
Posted by :
Archive Import (jacob) at 02:21 on Saturday, July 19, 2003
|
i want to add 9 hours and 28 minutes together in email_date field
select dateadd(hour,9,email_date) as date1 from support_email
select dateadd(minute,28,date1) from support_email
| |
Posted by :
Archive Import (shane) at 14:27 on Thursday, August 07, 2003
|
Ahh.. but what about more practical applications like if you had a timesheet program using asp and an access database and you needed to get the total hours worked. so you have a start time and an end time that a user enters in the ASP interface but when you insert into the db you want to enter the times plus a total?
sorry to ask but I am lost lost lost.
| |
Posted by :
Archive Import (cathy) at 00:31 on Saturday, August 16, 2003
|
Shane, if you have figured it out, please let me know. I'm in a similar situation.
If I find an answer, I'll come back and post it.
| |
|
|
Posted by :
sharky99x at 06:47 on Tuesday, November 11, 2003
|
Had problems when using the above for ASP but found that you need to put quotes around time/date value.
E.G
DATEADD("d",2,Now()) = In two days time
DATEADD("d",-2,Now()) = Two days ago
Best Regards
Sharky99x
| |
Posted by :
jermcode at 00:33 on Saturday, May 15, 2004
|
[quote]
i want to add 9 hours and 28 minutes together in email_date field
select dateadd(hour,9,email_date) as date1 from support_email
select dateadd(minute,28,date1) from support_email
[/quote]
just nest your functions:
[b]select dateadd(mi,28,dateadd(h,9,email_date)) as result_date from support_email[/b]
jeremy
| |
Posted by :
Laura at 09:40 on Thursday, August 25, 2005
|
Hi,
Can anyone help?
I want to set up a field in a table which adds 13 weeks to another field in a table (called aevents.date_received) automatically on each new record entry.
I would be grateful for any help.
Thanks,
Laura
| |
|
To post comments you need to become a member. If you are already a member, please log in .
| RELATED ARTICLES |
ASP Format Date and Time Script by Jeff Anderson
An ASP script showing the variety of date and time formats possible using the FormatDateTime Function. |
 |
Creating a Dynamic Reports using ASP and Excel by Jeff Anderson
A simple way to generate Excel reports from a database using Excel. |
 |
Create an ASP SQL Stored Procedure by Jeff Anderson
A beginners guide to setting up a stored procedure in SQL server and calling it from an ASP page. |
 |
ASP Shopping Cart by CodeToad Plus!
Complete source code and demo database(Access, though SQL compatible) to an ASP database driven e-commerce shopping basket, taking the user through from product selection to checkout. Available to CodeToad Plus! Members |
 |
Email validation using Regular Expression by Jeff Anderson
Using regular expression syntax is an exellent way to thoroughly validate an email. It's possible in ASP. |
 |
Creating an SQL Trigger by Jeff Anderson
A beginners guide to creating a Trigger in SQL Server |
 |
MagicGrid by Abhijeet Kaulgud
MagicGrid is an all-in-one grid for ASP programmers. It is a 3 Level Hierarchial Grid. You can Add, Edit, Delete Items under all the three levels. You can also cut-copy-paste Items from one level to other, It happens just by drag & drop! |
 |
The asp:checkbox and asp:checkboxlist control by David Sussman, et al
Checkboxes are similar to radio buttons, and in HTML, they were used to allow multiple choices from a group of buttons. |
 |
ASP.NET Forum Source Code by ITCN
Complete open source website Forum and Discussion Board programmed in Microsoft dot Net 1.1 Framework with Visual Basic. |
 |
The asp:listbox control by David Sussman, et al
The next HTML server control that we'll look at, <asp:listbox>, is very much related to <asp:dropdownlist>. |
 |
| |