Stupid Form Tricks
Dates and Calendars -
Getting Any Date You Want - Autofilling Date Fields

for most browsers

This script lets you do 2 things:
1. Get today's date in 9 different date formats
2. Get a past or future date using a reference date and the desired time difference (e.g. 30 days from 12/31/2002) in 9 different date formats.

The script with all the functions is available for copying in the textarea below. It starts by setting variables with today's date, month, and year.

var date = new Date();
var curr_dy = date.getDate(); 
var curr_mn = date.getMonth();
var curr_yr = date.getFullYear();

Use function todaysDate() to get, you guessed it, today's date. The function takes a numerical argument that corresponds to a date format in function dateFormats(). The formatting options are as follows:

0 = mmddyy
1 = mmddyyyy
2 = mm/dd/yyyy
3 = mm/dd/yy
4 = yyyymm
5 = ddmmyy
6 = dd/mm/yy
7 = ddmmyyyy
8 = dd/mm/yyyy

All other dates are produced using function getNextDate(). The function takes 5 arguments : month,date,year,increment,format. The increment should take the form of a positive or negative number and refers to the number of days before or after the date specified by the first 3 arguments. For example:

// - one day before January 1, 2004 in date format 2
getNextDate(0,1,2004,-1,2)

will produce the date 12/31/2003

Below in form name="myDates" are text inputs with dates in them. The scripts are shown beneath the text inputs.

<form name="myDates">
Today:format 2
Yesterday:format 2
Tomorrow:format 2
30 Days from Today:format 3
180 Days from Today:format 5
365 Days from Today:format 6
Day after 29/2/2004:format 8
Day before 1/3/2004:format 8

<script>
document.forms['myDates'].TODAY.value = todaysDate(2);
document.forms['myDates'].YESTERDAY.value = getNextDate(curr_mn,curr_dy,curr_yr,-1,2);
document.forms['myDates'].TOMORROW.value = getNextDate(curr_mn,curr_dy,curr_yr,+1,2);
document.forms['myDates'].THIRTY_DAYS.value = getNextDate(curr_mn,curr_dy,curr_yr,+30,3);
document.forms['myDates'].ONEEIGHTY_DAYS.value = getNextDate(curr_mn,curr_dy,curr_yr,+180,5);
document.forms['myDates'].ONE_YEAR.value = getNextDate(curr_mn,curr_dy,curr_yr,+365,6);
document.forms['myDates'].LEAP_PLUS.value = getNextDate(1,29,2004,+1,8);
document.forms['myDates'].LEAP_MINUS.value = getNextDate(2,1,2004,-1,8);
</script>
</form>






PXL8 2004