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:
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:
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:
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:
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:
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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
<!DOCTYPE html> <html lang="en"> <head> <title>Learning JavaScript Object</title> </head> <body> <script> //Sample 01: Create Today's Date var today = new Date(); document.writeln("Todays Date : " + today + "</br>"); //Sample 02: Create Date for Specific var aDate = new Date("10 Feb 2018"); document.writeln("Custom Date : " + aDate + "</br>"); //Sample 03: Print in Specific Format var DateFormatted = aDate.getDate() + "-" + ( aDate.getMonth() + 1) + "-" + aDate.getFullYear(); document.writeln("Formatted Date : " + DateFormatted + "</br>"); //Sample 04: Change Month to May aDate.setMonth(4); //Note Months are Zero Based aDate.setDate(30); document.writeln("Custom Date : " + aDate + "</br>"); //Sample 05: Pick the Date 17 Days from Today var TargetDate = new Date(); TargetDate.setDate(aDate.getDate() + 17); document.writeln("Custom Date : " + aDate + "</br>"); document.writeln("Target Date : " + TargetDate + "</br>"); </script> </body> </html> |
Categories: JavaScript
Tags: Date Object, getDate(), getFullYear(), getMonth()