This week's book giveaway is in the Jobs Discussion forum.
We're giving away four copies of Java Interview Guide and have Anthony DePalma on-line!
See this thread for details.
The moose likes HTML, CSS and JavaScript and the fly likes Add days to date() calculations Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Engineering » HTML, CSS and JavaScript
Bookmark "Add days to date() calculations" Watch "Add days to date() calculations" New topic

Add days to date() calculations

David Pettit

Joined: Dec 14, 2003
Posts: 2
I have some year, month, day, and time code that displays when a web page is accessed. However, I would also like some lines to figure out adding about 7 days to the curent date do I can display that. The trouble will be for going over a month into the next, and for going over a year into the next. Please provide a few lines for this of a location where I can find examples of all kinds of date and time calculations. Thank you.
Code I now use:
var this_weekday_name_array = new Array("Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday")//predefine weekday names
var this_month_name_array = new Array("January","February","March","April","May","June","July","August","September","October","November","December")//predefine month names
var this_date_timestamp=new Date()//get current day-time stamp
var this_weekday = this_date_timestamp.getDay()//extract weekday
var this_date = this_date_timestamp.getDate()//extract day of month
var this_month = this_date_timestamp.getMonth()//extract month
var this_year = this_date_timestamp.getYear()//extract year
var this_hour = this_date_timestamp.getHours() //extract hour dcp addon
var this_minute = this_date_timestamp.getMinutes() //extract minute dcp addon
var this_ampm = "PM"
var pad_hour = ""
var pad_minute = ""
var hold_hour = this_hour
if (this_year < 1000)
this_year+= 1900;//fix Y2K problem
if (this_year==101)
this_year=2001;//fix Netscape browsers - it displays the year as being the year 101!
if (this_hour < 12) //dcp addon
if (this_hour > 11) //dcp addon
if (hold_hour < 10)
if (this_minute < 10)
pad_minute = "0";
var this_date_string = this_weekday_name_array[this_weekday] + ", " + this_month + "/" + this_date + "/" + this_year + " " + hold_hour + ":" + pad_minute + this_minute + " " + this_ampm
<P> </P>
<P><font face="Verdana" size="2">From:  David Pettit</font></P>
<script language="JavaScript">document.write(this_date_string)</script>
Avi Abrami
Ranch Hand

Joined: Oct 11, 2000
Posts: 1141

Hi David,
Pardon me if I am stating the obvious, but are you not familiar with the GregorianCalendar class?
Have you seen these JavaWorld articles:
  • Calculating Java dates
  • Working in Java time

  • Whoops! I just realized that you are asking about JavaScript (and not java, so the above probably doesn't help you. In any case, perhaps it will be of help to someone using java (who doesn't know about the "GregorianCalendar" class ;-)
    Good Luck,
    [ December 14, 2003: Message edited by: Avi Abrami ]
    Dirk Schreckmann

    Joined: Dec 10, 2001
    Posts: 7023
    Welcome to JavaRanch, David!
    JavaScript is not Java, and Java is not JavaScript.
    I'm moving this thread to the HTML and JavaScript forum where they just might know a thing or two about this...

    [How To Ask Good Questions] [JavaRanch FAQ Wiki] [JavaRanch Radio]
    Eric Pascarello

    Joined: Nov 08, 2001
    Posts: 15385
    IO see I wrote this while I was asleep...I messed up with the fixedDate Line with the M/D/Y format....

    [ December 15, 2003: Message edited by: Eric Pascarello ]
    David Pettit

    Joined: Dec 14, 2003
    Posts: 2
    Eric, I will work with that to see how it comes out. Thank you.
    Eric Pascarello

    Joined: Nov 08, 2001
    Posts: 15385
    I edited the code above since I made a mistake
    I agree. Here's the link:
    subject: Add days to date() calculations
    It's not a secret anymore!