Working with dates and times in JavaScript

I need to get the current year and display it in a form. That way the form doesn’t have to be updated every year, just to change the date.

We always start with a date object, then use it to get what we need.

var today = new Date();
var theyear = today.getFullYear();
$("#theyear").html(theyear); // put the variable into a span with id of "theyear"

I would like to have a form that auto fills todays date in the following format: 2/27/2014

var currentTime = new Date();
var month = currentTime.getMonth() + 1;
var day = currentTime.getDate();
var year = currentTime.getFullYear();
document.write(month + "/" + day + "/" + year);

Here are great docs and examples from Mozilla

Leave a Reply