ArticleZip > Get Current Date In Dd Mon Yyy Format In Javascript Jquery

Get Current Date In Dd Mon Yyy Format In Javascript Jquery

When working on web development projects, displaying the current date in a specific format is a common requirement. If you're using JavaScript and jQuery, it's relatively easy to get the current date in the "dd mon yyyy" format. This format shows the day, abbreviated month, and full year, making it a useful way to present dates on your website or web application.

To achieve this in JavaScript and jQuery, you can follow these steps:

1. Create a Function: Start by creating a JavaScript function that will return the current date in the desired format. You can name this function something like `getCurrentDate`.

2. Get the Current Date: Within the function, you can create a new Date object in JavaScript to get the current date.

3. Format the Date: Once you have the current date, you can use JavaScript methods to extract the day, month, and year components from the date object.

4. Get Abbreviated Month: To get the abbreviated month name, you can create an array of month names and then use the getMonth() method to retrieve the month index.

5. Format the Output: Finally, combine the day, abbreviated month, and year components into a string formatted as "dd mon yyyy". Ensure that you add leading zeros to the day if required.

Here's an example implementation of the function:

Javascript

function getCurrentDate() {
  let months = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"];
  let currentDate = new Date();
  
  let day = currentDate.getDate().toString().padStart(2, '0');
  let month = months[currentDate.getMonth()];
  let year = currentDate.getFullYear();
  
  return `${day} ${month} ${year}`;
}

// Call the function to get the current date in "dd mon yyyy" format
let formattedDate = getCurrentDate();
console.log(formattedDate);

By calling the `getCurrentDate` function, you can now retrieve the current date in the required format and use it in your web project as needed.

Remember, JavaScript's Date object provides various methods to extract different date components, allowing you to customize the date representation based on your requirements. Using this approach with jQuery can enhance the interactivity and functionality of your website, making the user experience more engaging.

So, the next time you need to display the current date in "dd mon yyyy" format on your website or web app, you can easily achieve this using JavaScript and jQuery. Happy coding!

×