Breaking

Code Gripper
Responsive Advertisement
  • Home
Homedate

How to get current day date in JavaScript

byMahabubur Rahman -August 19, 2021
0

 

I have written a javascript function to solve this problem. Here I get the current date-time using the new Date() object. Then formate it.


Get Current Date Function:

function getCurrentDate(formate='mm/dd/yyyy'){

  var today = new Date();

  var currentDate = {};

  currentDate['dd'] = String(today.getDate()).padStart(2, '0');

  currentDate['mm'] = String(today.getMonth() + 1).padStart(2, '0'); //January is 0!

  currentDate['yyyy'] = today.getFullYear();



  today = formate.replace("mm", currentDate['mm']);

  today = today.replace("dd", currentDate['dd']);

  today = today.replace("yyyy", currentDate['yyyy']);


  return today;

}

Outputs:


console.log(getCurrentDate());

08/20/2021


console.log(getCurrentDate('dd/mm/yyyy'));

20/08/2021


console.log(getCurrentDate('dd-mm-yyyy'));

20-08-2021


console.log(getCurrentDate('mm-dd-yyyy'));

08-20-2021


console.log(getCurrentDate('mm-yyyy'));

08-2021


Source: Tech Tutorial




Tags date javascript
  • Facebook
  • Twitter

You may like these posts

Post a Comment

Previous Post Next Post

Follow Us

Popular Posts

CKEditor5

ModuleNotFoundError: No module named 'ckeditor_uploader'

byMahabubur Rahman-September 13, 2022

Subscribe Us

Facebook

Categories

Main Tags

Code Gripper

September 30, 2023

Total Pageviews

Search This Blog

CODE GRIPPER

Code Gripper
CODE GRIPPER

  • Home
  • Python
  • PHP

Menu Footer Widget

  • Home
  • About
  • Contact Us
Code Gripper

About Us

Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's.

Categories

Main Tags

Latest Posts

Most Popular

ModuleNotFoundError: No module named 'ckeditor_uploader'

September 13, 2022
Design by Blogger | Distributed by Gooyaabi
  • Home
  • About Us
  • Contact Us
  • RTL Version

Contact Form