Click to See Complete Forum and Search --> : A DATE related question?


bhatijay
01-03-2003, 02:54 PM
Hi all,

I have two questions:

1. How do I display the current date on top my web-site?
2. How do I display a date a couple of days in the future? Exg. if todays date is January 3, 2003 then I like to display a date as: Respond by January 6, 2003.

Thank you for your time.

BJ

Charles
01-03-2003, 03:05 PM
Remembering that this will fail one in ten times and that you should only use this method if the date is unimportant to the page

<script type="text/javascript">
<!--
document.write(new Date());
// -->

will give you the current date and time and

</script><script type="text/javascript">
<!--
TICS_PER_DAY = 86400000;
document.write(new Date((new Date()).getTime() + TICS_PER_DAY));
// -->
</script>

will give you the date and time in 24 hours. The format isn't too friendly but if you put something like the following in the document's head element then you can overwrite the format for all JavaScript dates on the page.

<script type="text/javascript">
<!--
Date.prototype.toString = function () {return [['Sunday,', 'Monday,', 'Tuesday,', 'Wednesday,', 'Thursday,', 'Friday,', 'Saturday,'] [this.getDay()], this.getDate(), ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'] [this.getMonth()], this.getFullYear()].join('&nbsp;')}
// -->
</script>

khalidali63
01-03-2003, 03:07 PM
below is the code that will give you todays date.you can position it anywhere using CSS.
and for the date in future ,just parse the date in do a bit of calculations and then print it

Khalid

<body>
<script>
function printDate(){
var date = new Date();
document.getElementById("today").innerHTML = date;
}
onload=printDate;
</script>
<div id="today" style="position:absolute;"></div>

</body>

bhatijay
01-03-2003, 03:14 PM
Thank you all

BJ

Charles
01-03-2003, 07:44 PM
Dave's example isn't really different from mine, except that it makes clear what is going on. But perhaps we could clean this up just a bit and be consistent with the object oriented nature of JavaScript and with the Java naming conventions. Which, except for event handlers, JavaScript follows. (See http://java.sun.com/docs/codeconv/html/CodeConventions.doc8.html.)

Put the following in the document's head. Omit or edit the Date.prototype.toString method as desired.

<script type="text/javascript">
<!--
Date.MILLI_SECOND = 1;
Date.ONE_SECOND = 1000 * Date.MILLI_SECOND;
Date.ONE_MINUTE = 60 * Date.ONE_SECOND;
Date.ONE_HOUR = 60 * Date.ONE_MINUTE;
Date.ONE_DAY = 24 * Date.ONE_HOUR;
Date.ONE_WEEK = 7 * Date.ONE_DAY;
Date.ONE_YEAR = 365 * Date.ONE_DAY;

Date.prototype.toString = function () {return [['Sunday,', 'Monday,', 'Tuesday,', 'Wednesday,', 'Thursday,', 'Friday,', 'Saturday,'] [this.getDay()], this.getDate(), ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'] [this.getMonth()], this.getFullYear()].join(' ')}
// -->
</script>

And then use one of the following:

<script type="text/javascript">
<!--
document.write(new Date((new Date()).getTime() + Date.ONE_DAY));
// -->
</script>

<script type="text/javascript">
<!--
now = new Date();
document.write (new Date(now.getTime() + Date.ONE_DAY));
//-->
</script>

<script type="text/javascript">
<!--
usrDate = new Date();
usrDate.setTime(usrDate.getTime() + (2 * Date.ONE_WEEK));
document.write(usrDate);
// -->
</script>

David Harrison
01-04-2003, 09:39 AM
This was one of my first experiments with javascript so I'll admit I nicked a lot of the source from other ppl (thanks to www.javascriptsource.com). I wrote and time & date script so if you don't want the time just delete the bits you don't want.