JavaScript Date Format DD-MMM-YYYY
Last Updated : Mar 11, 2024
IN - JavaScript | Written & Updated By - Pragati
In this tutorial we will show you the solution of JavaScript date format dd-mmm-yyyy, here we needs to use external jquery library support file and match method for gets expected date format.
The match method is an inbuilt method which is used to search a string for a match against any regular expression.
If the match is found, then this will return the match as an array.
Step By Step Guide On JavaScript Date Format DD-MMM-YYYY :-
Here we defined button and p element. In script we defined click event will loads when button clicks by user, there we defined date string and stored on variable ‘str’ next we needs to append that variable with match() method.
In match method we defined regular expression as needed format then it will search with ‘str’ if they match then it returns result as array and it will stored on variable ‘res’ then we appended on html page.
<!DOCTYPE html> <html> <head> <title>DATE DD-MMM-YYYY FORMAT</title> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script> </head> <body> <button>Click Here</button> <p></p> <script> $("button").click(function(){ var str="24-Sep-1998"; var res=str.match(/\d{2}-(Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec)-\d{4}/gi); $("p").html(res); }) </script> </body> </html>
- <!DOCTYPE html> tag which is instruct the web browser about what version of HTML file written in and it’s not have any ending tag.
- The<html> tag is used to indicate the beginning of HTML document.
- As above shown <head> tag is contain information about webpage and external file links are declared here. <title> tag is used for set the webpage title.
- Here we needs to import external jquery libraries supports because in script block we used jquery for define date format.
- Both <head> and <title> tags having their pair end tag, so we need to close the ending tags respectively. If you’re not closed anyone of ending tag properly that is also affect the webpage result.
- <body> tag is beginning of main coding part because it contain coding of entire website blocks and elements described here.
- Here we defined button and <p> para tag elements. The button element used for generate click event when user clicks on it and para tag used for append the result array on webpage.
- In script we defined button with click event for find when user clicks on it then it loads function, there we defined date string ‘24-Sep-1998’ and stored on variable ‘str’.
- As we seen like earlier in match method we defined regular expression in expected date format then string ‘str’ appended with match method now which is search everything with regular expression to finds whether matches everything.
- If they matched with our defined string then result string stored on variable ‘res’ otherwise it throws error. At last we appended result ‘res’ on html webpage with the help of para <p> tag.
- Both </body>,</html> tags closed respectively. </body> tag indicates the end of body, Then </html> tag indicates the end of HTML document.
Conclusion :-
In conclusion now we are able to know how to define date in ‘dd-mmm-yyy’ format using javascript.
When we executes program on browser we can see button ‘Click Here’ user needs to click this button then defined date string will display on webpage.
If you’re needs to change date string ‘str’ just modify that with some other dates then we gets output accordingly but needs to confirm one thing that ‘-’ and format needs to match with above seen otherwise it throws error because our regular expression defined based on that.
I hope this tutorial on JavaScript date format dd-mmm-yyyy helps you and the steps and method mentioned above are easy to follow and implement.