February 19, 2001 - Forward-Computing the Date | WebReference

February 19, 2001 - Forward-Computing the Date

Yehuda Shiran February 19, 2001
Forward-Computing the Date
Tips: February 2001

Yehuda Shiran, Ph.D.
Doc JavaScript

In many applications you need to forward-compute the date. As an example, a worker in a library with a 25-day return policy needs to know when a book is due. A pharmacist needs to compute the expiration date of a prescription drug with 63 day shelf period. A high school student wants to find out when his 28 day curfew ends, so he can start dating again. These are just a few examples where people need to forward compute the date. Luckily, JavaScript provides you with a wealth of Date methods. The trick is to create a Date object and to add the required number of days to its date property. Note that the number of days is expressed in milliseconds. Here is an example that demonstrates the library example above:

<SCRIPT LANGUAGE="JavaScript">
var now = new Date();
now.setTime(now.getTime() + 25 * 24 * 60 * 60 * 1000);
</SCRIPT>

Click here to try it.