Displaying Date with format
The output is a number based on the todays date.
Formatting the dateWe will try to format this date returned by the above getDate() method. Our formatting will depend on the last digit of the month returned by the above getDate function. If the last digit is 1 then we will add 'st' to the end like 21st , it is “nd” for 2, “rd” for 3, “th” for 4 and other digits. To get this format we will use if and if, else condition to know what should be used based on the last digit. We will get the last digit of the day returned by dividing it with 10 and taking the quotient. This way from 21 we will get 1 and from 17 we will get 7. Here is the code to get the last digit from any number.
Here my_date variable stores the date returned by getDate function. We will add the ending format to the date value and display the value in an alert box.
Here is the full code of the above demo.
Subscribe to our YouTube Channel here
This article is written by plus2net.com team.
|
▼ More on JavaScript Date |