1024px-Flatirons_Winter_Sunrise_edit_2

Getting the current date in JavaScript is a piece of cake. The language features a convenient Date object that’s preloaded with all sorts of methods for retrieving temporal values. The syntax is simple enough and the names of the methods, for the most part, are pretty intuitive. In the following examples, I’ll show you how to work with JavaScript’s Date object to get the current year, month, day, time… down to the millisecond!

Getting the current date:

To get the current date in JavaScript, simply use the new keyword to create a fresh instance of JavaScript’s Date object prototype.

What gets logged to the console here will look something like this:

That’s the current day of the week, the current month, the current day of the month, the current year, and even the current time in 24 hour format. The “GMT” bit stands for Greenwich Mean Time; and the -0500 is your local time’s offset in hours from GMT. Quoting from Wikipedia, “Greenwich Mean Time (GMT) is the mean solar time at the Royal Observatory in Greenwich, London.” It’s related to Coordinated Universal Time (UTC). The “Eastern Standard Time” in the logged date value refers to your browser’s/computer’s time zone.

*Note that GMT and UTC have been reported to differ. We’ll look at UTC value’s in a minute here.

*Also note (from MDN’s Date object link referenced above) “The JavaScript date is based on a time value that is milliseconds since midnight 01 January, 1970 UTC. A day holds 86,400,000 milliseconds. The JavaScript Date object range is -100,000,000 days to 100,000,000 days relative to 01 January, 1970 UTC.”

*And note further that the values retrieved using the aforementioned method, are dependent on your computer’s/device’s system settings. Change your computer’s clock settings, and you’ll change what time the method will return.

Alright, date’s all dressed up and ready to go! So how do we parse it?

Getting the day of the week:

Assume that we’re still using our newly created date variable. To get the day of the week, simply use the JavaScript Date object’s getDay() method.

*Note that the days of the week are enumerated 0 through 6, with Sunday = 0, Monday = 1, Tuesday = 2, and so on.

Getting the month:

Again using the date variable; to get the current month of the current year, simply use the getMonth() method (I told you the method names are intuitive!).

*Note that, similar to the days of the week, the days of the month are returned in numerical fashion; 0 through 11. January = 0, February = 1, March = 2, and so on.

Getting the day of the month:

To get the day of the month, you can use the getDate() method.

*Beware! This one bucks the trend. With the getDate() method, the days of the month are numbered 1 through 31; the number 1 corresponds to the first day of the month.

Getting the time:

To get the current time… yep, you guessed it… Use the getTime() method.

Looks… weird… What gives? Well, remember the *Also Note from earlier? And remember how I said I’d show you how to get the current date in JavaScript down to the millisecond? That’s what you’re looking at here. You’re looking at the number of milliseconds since January 1, 1970 00:00:00 UTC. So how many years is that?

1454461242058 ms / 31540000000 ms/year = about 46 years

1970 + 46 years = the year 2016

Hoorah! Let’s check it out to be sure…

Yep, there it is! We just showed our JavaScript Date to a wonderful night out on the town! We spent our evening reflecting on the years, months, days, hours, minutes, and seconds, er… milliseconds… gone by. How romantic is that?
Code Examples: http://codepen.io/anon/pen/EPpMzP?editors=0012