View Full Version : Customized Clock/Date

01-29-2009, 12:53 AM

I'm pretty much a newbie when it comes to javascript and I have a small request for anyone who is willing to help me on this.

All I need is a simply date script, but a bit customized.

Basicly the script should show date as "January 1, 2009" in a website, but it wouldn't show the actual date. It would show a custom date.

To calculate the date shown, three dates are used:

D1 (date 1) is the current date
D2 is a pre-set date (for example 1.1.1990)
D3 is also a pre-set date (eg 1.1.2000)

The final date is D1-D2 (difference between the two dates...in days), multiplied by X (a positive whole number) and added to D3 (date 3).

For the sake of example, if:
D1 is today (29. January 2009)
D2 is 28. January 2009
D3 is 1. January 2000
and the number X has a value of 2

the final date would be 3. January 2000. The difference between D1 and D2 is 1 day, multiplied by 2...equals two days. Added to the D3 makes it January 3rd.

Thanks anyways

Philip M
01-29-2009, 07:59 AM
Here you are:-

<script type = "text/javascript">
var months = new Array ("January", "February", "March", "April", "May", "June", "July", "August", "September","October", "November", "December");
var x = 2;
var now = new Date(); // today
var D1 = now.getTime();
var D2 = now.setFullYear(2009,0,28); // YYYY,MM,DD - months are 0-11
var diff = D1 - D2;
diff = diff * x;
var D3 = now.setFullYear(2000,0,1); // months are 0-11
var finalDate = D3+diff;
var d = new Date(finalDate);
var mm = d.getMonth();
var dd = d.getDate();
var yy = d.getFullYear();
finalDate = (dd + " " + months[mm] + " " + yy);

alert (finalDate); // 3 January 2000

"In the beginner's mind there are many possibilities, but in the expert's mind there are few” Shunryu Suzuki (Japanese Zen priest, ?-1971)