Programming Examples

Are you a Programmer or Application Developer or a DBA? Take a cup of coffee, sit back and spend few minutes here :)

JavaScript Date Object

1. JavaScript Date Object

The JavaScript Date Object helps in dealing with a date. Using this object, we can get the current date, perform date related calculations, and convert a date into a string object. In this JavaScript Example, we will learn about the basic functionalities of the Date Object.

2. Display Today’s Date

In JavaScript, sometimes we need to create date object to represent Today’s date. The below example gets today’s date and displays it:

Fig 1. JavaScript Display Todays Date
Fig 1. JavaScript Display Todays Date

1) The Date object construction with empty parameters constructs the date which represents today’s date. Here, the variable today stores the current date.
2) We print the current date in the browser window.
3) Output shows today’s date in GMT 5.30 format. It depends on which date format the browser is set.

3. Create Date Object for a Given Date

In the last section, we saw the empty date constructor represents today’s date. The below example shows how to create JavaScript Date instance for a specific date:

Fig 2. JavaScript Create Date Object for Specific Date
Fig 2. JavaScript Create Date Object for Specific Date

1) Here the date constructor takes the string form of the date. Note, the day of the month is in two digits and the year is in 4 digits. Also note, we specify the month in 3 letters abbreviated string. The constructor returns the date, and we store that in a variable called aDate.
2) Here, we print the date in the browser window.
3) We can see the date in the browser displays day of a month, month, and year. Also note, the display includes the weekday as well.

4. Formatting a JavaScript Date Object

We can by-part the date and stitch it to the way we want. It is easy to format the date in our custom style and below is the example:

Fig 3. JavaScript - Formatting Date Object
Fig 3. JavaScript – Formatting Date Object

1) In the previous section, we created a date instance and stored that in a variable called aDate. The method getDate() returns the day number in a month. We prefix this with a hyphen.
2) The method getMonth() of the JavaScript’s Date object returns a month number. The month number is zero based. Means, 0 stands for JAN and 11 stands for DEC. So, we add +1 from the getMonth return result. JavaScript return zero based month number so that you can easily retrieve the string form a month from a custom array. Here, in our example, we print the month in number form.
3) Method getFullYear() returns the year in 4-digit format. We append all these date-by-part function return values to form a formatted string called DateFormatted.
4) Shows the Date printed in DD-MM-YYYY format.

5. Change Specific Date Part

In the previous example, we used the get version of Date By-Part functions. Here, we use the set version to change a specific part of the date:

Fig 4. JavaScript - Change Date Parts
Fig 4. JavaScript – Change Date Parts

1) We use the setMonth method to change the month as MAY. Note, the months are zero based and hence we pass the value of 4.
2) Here, we set the day of the month as 30 by calling the function setDate.
3) Shows the result of the changed date. In the output, we can observe the month is displaying as MAY and Month Day is 30.

6. Calculate Date with ‘N’ Days Offset

Sometimes we may end up calculating the date ‘N’ days from today. The below example shows how we get the date 17 days away from today:

Fig 5. JavaScript - Calculate Date by Day Count
Fig 5. JavaScript – Calculate Date by Day Count

1) We already have a Date instance in a variable aDate which is from the previous section of the code. Now, we create one more instance and store that in TargetDate variable. Then, we add 17 days to the aDate instance and pass that as a parameter to the setDate function. The target date now represents 17 days future from the aDate instance.
2) Here, we print both date variable for a comparison.
3) The output shows the Target date is 17 days past the base date. Note, at step 1, we can also do the negation if we want to get the date in the past.

7. Code Reference

Categories: JavaScript

Tags: , , ,

Do you like this Example? Please comment about it for others!!

This site uses Akismet to reduce spam. Learn how your comment data is processed.