• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Add days to date() calculations

 
David Pettit
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
this_ampm="AM";
if (this_hour > 11) //dcp addon
hold_hour=this_hour-12;
else
hold_hour=this_hour;
if (hold_hour < 10)
pad_hour="0";
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
Posts: 1141
1
Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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,
    Avi.
    [ December 14, 2003: Message edited by: Avi Abrami ]
     
    Dirk Schreckmann
    Sheriff
    Posts: 7023
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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...
     
    Eric Pascarello
    author
    Rancher
    Posts: 15385
    6
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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
    Greenhorn
    Posts: 2
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Eric, I will work with that to see how it comes out. Thank you.
     
    Eric Pascarello
    author
    Rancher
    Posts: 15385
    6
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    I edited the code above since I made a mistake
    Eric
     
    • Post Reply
    • Bookmark Topic Watch Topic
    • New Topic