?
Solved

ASP/JAVASCRIPT : Date Manipulation

Posted on 2005-04-20
11
Medium Priority
?
1,626 Views
Last Modified: 2008-03-06
Hi experts,

I need an efficient function to easily add or substract a certain number of days, months, or years to a given date.
0
Comment
Question by:DesertWarrior
  • 5
  • 3
  • 2
  • +1
11 Comments
 
LVL 2

Expert Comment

by:RickDai
ID: 13829131
Hmm i am not sure your exact problem. I may be wrong but try:
Function ManipDate(str_date, value, exp)
      dim return_val
      Select Case exp
            Case "+"
                  return_val = str_date + value
            Case "-"
                  return_val = str_date - value
            Case else
                  return_val = str_date
      End Select
      ManipDate = return_val
End Function

<%= ManipDate(date(), 30, "+") %>
0
 
LVL 5

Author Comment

by:DesertWarrior
ID: 13829339
That cannot work.
How does the function know if the "value" is a number of days, months or years?
0
 
LVL 2

Expert Comment

by:RickDai
ID: 13829353
What is providing the # of days/months/years?

if it is User provided then it can be converted to # of days.
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 2

Expert Comment

by:RickDai
ID: 13829366
Also I did not notice but it seems there is a built in function
DateAdd("d",30,date())

In this case that should be what you're looking for. Now you can add to the day, month or year.

more detail can be found here
http://www.w3schools.com/asp/showasp.asp?filename=demo_asp_dateadd
0
 
LVL 22

Expert Comment

by:neeraj523
ID: 13831884
Hello DesertWarrior

if you want it to be done at server side.. Why don't you use Vb default function named DatAdd()

here are few examples of using this function

'Add 1 day to the current date
Response.Write DateAdd("day",1,Date())

'substract  1 day to the current date
Response.Write DateAdd("day",-1,Date())

same way you can use month, year, hours etc  to add or substract values..

for more ingo check these links

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/script56/html/vsfctdateadd.asp
http://www.w3schools.com/vbscript/func_dateadd.asp

hope it will help u out..

neeraj523
0
 
LVL 5

Author Comment

by:DesertWarrior
ID: 13833978
DateAdd() looks like THE function I need but the problem is I need it in JAVASCRIPT... Anyone knows of an equivalent function in javascript?
0
 
LVL 2

Accepted Solution

by:
RickDai earned 2000 total points
ID: 13834939
<script language="javascript">
<!--
function dateAdd(intval, numb, d){
      /*intval is YYYY, M, D, H, N, S as in VBscript; numb is amount +/-; base is javascript date object*/
      var base = new Date(d);
      switch(intval){
            case "M":
                  base.setMonth(base.getMonth() + numb);
                  break;
            case "YYYY":
                  base.setFullYear(base.getFullYear() + numb);
                  break;
            case "D":
                  base.setDate(base.getDate() + numb);
                  break;
            case "H":
                  base.setHours(base.getHours() + numb);
                  break;
            case "N":
                  base.setMinutes(base.getMinutes() + numb);
                  break;
            case "S":
                  base.setSeconds(base.getSeconds() + numb);
                  break;
            default:
      }
      return base
}
//-->
</Script>

<script language="javascript">
      document.write(dateAdd("D", 2, "12/31/1998 23:59:59") + "<br>");
</script>

Note!!! if you do not specifiy a time it will return by default 00:00:00 for time.
0
 
LVL 22

Expert Comment

by:neeraj523
ID: 13840349
Hello DesertWarrior

still looking any further help ??

neeraj523
0
 
LVL 5

Author Comment

by:DesertWarrior
ID: 13843191
thank you all
Rickdai supplied the right function to use with javascript
0
 
LVL 2

Expert Comment

by:RickDai
ID: 13843229
Thank you. glad to have helped.
0

Featured Post

Prep for the ITIL® Foundation Certification Exam

December’s Course of the Month is now available! Enroll to learn ITIL® Foundation best practices for delivering IT services effectively and efficiently.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I recently decide that I needed a way to make my pages scream on the net.   While searching around how I can accomplish this I stumbled across a great article that stated "minimize the server requests." I got to thinking, hey, I use more than one…
I have helped a lot of people on EE with their coding sources and have enjoyed near about every minute of it. Sometimes it can get a little tedious but it is always a challenge and the one thing that I always say is:   The Exchange of informatio…
This Micro Tutorial will teach you how to add a cinematic look to any film or video out there. There are very few simple steps that you will follow to do so. This will be demonstrated using Adobe Premiere Pro CS6.
Despite its rising prevalence in the business world, "the cloud" is still misunderstood. Some companies still believe common misconceptions about lack of security in cloud solutions and many misuses of cloud storage options still occur every day. …
Suggested Courses
Course of the Month17 days, 12 hours left to enroll

830 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question