Warning: session_start(): open(/tmp/sess_805142144d90a2593face588da52f707, O_RDWR) failed: Disk quota exceeded (122) in /home/wvyrfnwn/learnshareit.com/wp-content/plugins/learnpress/inc/class-lp-page-controller.php on line 1007

Warning: session_start(): Failed to read session data: files (path: /tmp) in /home/wvyrfnwn/learnshareit.com/wp-content/plugins/learnpress/inc/class-lp-page-controller.php on line 1007

Warning: ftp_nlist() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 420

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_nlist() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 420

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 719

Warning: ftp_nlist() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 420

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_nlist() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 420

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 719

Warning: ftp_mkdir() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 562

Warning: ftp_nlist() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 420

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230
How To Get Month And Date In 2 Digit Format In JavaScript - LearnShareIT

How To Get Month And Date In 2 Digit Format In JavaScript

Get Month and Date in 2 Digit Format in JavaScript

There are many ways to get month and date in 2 digit format in JavaScript. In this article, I will introduce two ways to use conditional statements. Another way is to use some built-in functions.

How to get Month and Date in 2 digit format in JavaScript

Use conditional statements

We can use a conditional statement to quickly check the Month and Date in 2 digit format.

Example:

function getDateMonth2Digit(dateCheck) {
    let day = dateCheck.getDate();
    if (day < 10) {
      	day = '0' + day;
    }

    let month = dateCheck.getMonth() + 1;
    if (month < 10) {
      	month = '0' + month;
    }

    return (day + '/' + month + '/' + dateCheck.getFullYear());
}

var date1 = new Date('2020/3/1');
var date2 = new Date('2020/11/23');

console.log(getDateMonth2Digit(date1));
console.log(getDateMonth2Digit(date2));

Output:

01/03/2020
23/11/2020

In the above example, I create a function that checks the passed date and outputs Month and Date in 2 digit format with the input parameter of a date we need to check and change the date format to DD/MM. The function will first get the Day in the passed dateCheck variable by the getDate() method in JS, then compare it with 10. If less, we will add a leading 0 by adding the character '0'. We will get the number of days formatted with two characters. For the Month, it is no different. Still, each time you use to date, you must add one because, in the getMonth() method, the winning number is represented (0->11). Finally, we output a string that is the date we need to check with the format DD/MM/YYYY.

Use the padStart() method  

The padStart() method is a built-in function in Javascript, which pads a string in front of a number and converts it to a string. The return type will be a string with the number of characters as the parameter we pass in the Initial number and parameter padding string.

Syntax:

padStart(totalLength, padString)

Parameters:

  • totalLength: This is the total length of the string you want to add padding string.
  • padString: This is the string you want to add when the number of characters is not equal to totalLength.

Example:

function getDateMonth2Digit(dateCheck) {
    let day = String(dateCheck.getDate()).padStart(2, "0");
    let month = String(dateCheck.getMonth() + 1).padStart(2, "0");
    return (day + '/' + month + '/' + dateCheck.getFullYear());
}

var date1 = new Date('2020/3/2');
var date2 = new Date('2020/10/22');

console.log(getDateMonth2Digit(date1));
console.log(getDateMonth2Digit(date2));

Output:

02/03/2020
22/10/2022

After running the above function, we see that the return result is a date in the format DD/MM/YYYY with days less than 10. By default, we will add zeros to the beginning of the day with the padStart() method. Similarly, the program adds zeros to the beginning of the month that are less than 10. After processing, we combine the string and output the date formated as DD/MM/YYYY.

Summary

I have shown you several ways to get Month and Date in 2 digit format in JavaScript in the above tutorial. To understand and apply, you should practice; this will help you understand and remember it longer. I hope this article is helpful for you. Good luck for you!

Maybe you are interested:

Leave a Reply

Your email address will not be published. Required fields are marked *