The Sysadmin Notebook  

Sitemap

Date Objects

Manipulating Date Objects

A date object should be declared:

var myDate = new Date(myYear, myMonth, myDate, myHour, myMinute, mySecond, my Millisecond)

You only need to specify the parameters to the level of accuracy you want. For example

myDate = (2008, 1, 9)

Month values start at zero for January

Use the get methods to extract the date parts:

myMonth = myDate.getMonth(); myYear = myDate.getFullYear(); myMinutes = myDate.getDate()

Use the corresponding set methods to change the date

<script type="text/javascript">
var myDate = new Date(prompt('Please enter a date: \(eg: 1 Jan 2007\)',''));
document.write('Your date: ' + myDate + "<br />");
myDate.setMonth((myDate.getMonth() + 1));	
document.write('Date in a months time: ' + myDate + '<br />');
</script>
</p>