🔥 $100K Hit! Where Will Bitcoin Go Next? Find Out Live!

Code has been added to clipboard!

Date Functions

Reading time 5 min
Published Aug 10, 2017
Updated Oct 10, 2019

JavaScript Date Functions: Main Tips

  • Date is an object meant for displaying and editing time and dates.
  • To create a date object, you use new Date().
  • There are many JavaScript date funtions that are used to manipulate the way time-related information is to be displayed.

Date Manipulation Explained

Date manipulation should not be underestimated: it is one of the most common features that websites have. To avoid misunderstandings and mistakes, you should learn about different JavaScript date functions. You will see that many methods can be used to JavaScript get date.

For instance, the regular JavaScript get date function will return a value, indicating the day of the month. Now, the getDay() function is another method to retrieve information about date: it will generate a value, representing the day of the week. It is done according to the universal time. What does that mean, you ask?

All of the functions designed to complete the JavaScript get date task will deliver results according to either local or universal time. The local time refers to the time and date you see on the lower right corner of your computer screen. Therefore, you can also retrieve JavaScript today's date. Coordinated Universal Time (UTC) is used as universal time.

To find more JavaScript date functions to output information about time and time, scroll down to the last section of this tutorial.

We already have two tutorials on date and time in JavaScript. We recommend that you start by learning more about how date format JavaScript is set to present as output.

We also encourage you to read how new date objects should be created. See an example below:

Example
var date = new Date(); 
var date = new Date(milliseconds); 
var date = new Date(dateString); 
var date = new Date(year, month, day, hours, minutes, seconds, milliseconds);

DataCamp
Pros
  • Easy to use with a learn-by-doing approach
  • Offers quality content
  • Gamified in-browser coding experience
  • The price matches the quality
  • Suitable for learners ranging from beginner to advanced
Main Features
  • Free certificates of completion
  • Focused on data science skills
  • Flexible learning timetable
Udacity
Pros
  • Simplistic design (no unnecessary information)
  • High-quality courses (even the free ones)
  • Variety of features
Main Features
  • Nanodegree programs
  • Suitable for enterprises
  • Paid Certificates of completion
edX
Pros
  • A wide range of learning programs
  • University-level courses
  • Easy to navigate
  • Verified certificates
  • Free learning track available
Main Features
  • University-level courses
  • Suitable for enterprises
  • Verified certificates of completion

Object Properties

When discussing JavaScript date functions, it is important to remember the date object. They can have two properties. The first one called prototype. It is applied when developers want to assign different properties and methods to the date object.

The second one is known as the constructor indicates the function that was used to create the prototype of the date object.

Property Description
prototype Assigns methods and properties to an object.
constructor Returns the function that generated the date object's prototype.

Variety of Date Functions

It is easy to get lost in the long list of JavaScript date functions. However, we have prepared a simple table you can refer to anytime. Each method is explained in a clear and concise manner. Therefore, it's easier for you to choose which option suits your needs best. Take a look:

Method Description
getDate() Returns the day of the month (1-31).
getFullYear() Returns the year
getDay() Returns the day of the week (0-6).
getHours() Returns the hour (0-23).
getMilliseconds() Returns the milliseconds (0-999).
getMonth() Returns the month (0-11).
getMinutes() Returns the minutes (0-59).
getTime() Returns Unix time in milliseconds.
getSeconds() Returns the seconds (0-59).
getUTCDate() Returns the day of the month according to UTC (1-31).
getTimezoneOffset() Returns the time difference between UTC time and local time in minutes.
getUTCFullYear() Returns the year according to UTC.
getUTCDay() Returns the day of the week according to UTC (0-6).
getUTCMilliseconds() Returns the milliseconds according to UTC (0-999).
getUTCHours() Returns the hour according to UTC (0-23).
getUTCMinutes() Returns the minutes according to UTC (f-59).
getUTCSeconds() Returns the seconds according to UTC (0-59).
getUTCMonth() Returns the month according to UTC (0-11).
now() Returns Unix time in milliseconds.
getYear() DEPRECATED. Use the getFullYear() method instead.
setDate() Sets the day of the month of a date object.
parse() Parses a date string and returns the Unix time in milliseconds.
setHours() Sets the hour of a date object.
setFullYear() Sets the year of a date object.
setMinutes() Sets the minutes of a date object.
setMilliseconds() Sets the milliseconds of a date object.
setSeconds() Sets the seconds of a date object.
setMonth() Sets the month of a date object.
setUTCDate() Sets the day of the month of a date object according to UTC.
setTime() Sets a date to a specified Unix time in milliseconds.
setUTCHours() Sets the hour of a date object according to UTC.
setUTCFullYear() Sets the year of a date object according to UTC.
setUTCMinutes() Set the minutes of a date object according to UTC.
setUTCMilliseconds() Sets the milliseconds of a date object according to UTC.
setUTCSeconds() Set the seconds of a date object according to UTC.
setUTCMonth() Sets the month of a date object according to UTC.
toDateString() Converts the date portion of a date object into a readable string.
setYear() DEPRECATED. Use the setFullYear() method instead.
toISOString() Returns the date as a string using the ISO standard.
toGMTString() DEPRECATED. Use the toUTCString() method instead.
toLocaleDateString() Returns the date portion of a date object as a string using locale conventions.
toJSON() Returns the date as a string formatted as a JSON date.
toLocaleString() Converts a date object to a string using locale conventions.
toLocaleTimeString() Returns the time portion of a date object as a string using locale conventions.
toTimeString() Converts the time portion of a date object to a string.
toString() Converts a date object to a string.
UTC() Returns Unix time in milliseconds according to UTC.
valueOf() Returns the primitive value of a date object.
toUTCString() Converts a date object to a string according to UTC.