Search results

JavaScript: Dates

JavaScript has a built-in Date object that allows you to use it in various ways.

To use the Date object, you assign a variable as a new Date, like this:

var eclipse = new Date();

The Date object accepts six parameters: year, month, day, minutes, seconds, and milliseconds. You don't need to use all the parameters.

If you don't insert any values in the parentheses (), the parameters default to the current date and time.

Example Use of the Date Object

To set a date to 12/12/12, you might write:

var eclipse = new Date(2012,12,12);

Now you can apply various methods to eclipse to manipulate the Date object. For example, let's suppose you want to know what day of the week this is. You would write:

var eclipse = new Date(2012,12,12);
//apply the method to the object
console.log( eclipse.getDay() );

The result is 6. This means that 12-12-2012 falls on a Friday. (Remember that the first day of the week is a 0, not a 1.)

Here's another example:

//set your a variable for sampleDate
var sampleDate = new Date(1975,9,2);
//apply the getDay method to the object
console.log ( sampleDate.getDay() );

This returns 3, which corresponds to Wednesday.

Available Methods for the Date Object

The Date object has a variety of methods:

.getMonth();// returns a number from 0 to 11
.getFullYear(); //YYYY (not a zero-based index)
.getDate(); // day of the month
.getDay(); // 0-6 day of the week. 0 is sunday
.getHours(); // returns 0-23
.getTime(); // milliseconds since 1/1/1970

The getTime Method

The .getTime method seems odd (returning milliseconds since 1970) but is actually useful in conditional statements. You can't ask if two dates are equal to each other, because dates aren't regular numbers. However, you can ask which date has a larger number of milliseconds since 1970.

Essentially, the getTime method is a way of converting dates to numbers.

Here's how you might use getTime:

//create two date objects
var newYearsDay = new Date(1,1,2013);
var valentinesDay = new Date(2/14/2013);
//if the first object is greater than the second, write this
if ( newYearsDay.getTime() ) > valentinesDay.getTime() ) {
console.log("Great!") }
//if it's not, write this
else {
console.log("That's okay.")

Using the setDate Method

In addition to .getDate, there are equivalent methods for setting the date using setDate. The following are available methods for setDate:

.setMonth(); // sets a number from 0 to 11
.setFullYear(); //sets the year (not a zero-based index)
.setDate(); // sets the day of month
.setDay(); // sets the day of the week (0-6)
.setHours(); //sets the hours for a day (0-23)
.setTime(); // sets the milliseconds since 1/1/1970.

About Tom Johnson

Tom Johnson

I'm a technical writer / API doc specialist based in the Seattle area. In this blog, I write about topics related to technical writing and communication — such as software documentation, API documentation, visual communication, information architecture, writing techniques, plain language, tech comm careers, and more. Check out my API documentation if you're looking for more info about that. If you're a technical writer and want to keep on top of the latest trends in the field, be sure to subscribe to email updates below. You can also learn more about me or contact me. Finally, note that the opinions I express on my blog are my own points of view, not that of my employer.