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

number

How to get only number from string in Python?

byMahabubur Rahman-September 10, 2021

ModuleNotFoundError: No module named 'psycopg2' django

September 17, 2021

How to change the date format in Laravel view page

September 11, 2021

How to get current day date in JavaScript

August 19, 2021

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

How to get only number from string in Python?

September 10, 2021

ModuleNotFoundError: No module named 'psycopg2' django

September 17, 2021

How to change the date format in Laravel view page

September 11, 2021
Design by Blogger | Distributed by Gooyaabi
  • Home
  • About Us
  • Contact Us
  • RTL Version

Contact Form