Warning: session_start(): open(/tmp/sess_5bdacd2fabca28dbe93e36505b02bcb9, 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 Validate A Date Formatted As DD/MM/YYYY In JavaScript - LearnShareIT

How To Validate A Date Formatted As DD/MM/YYYY In JavaScript

Validate a Date formatted as DD/MM/YYYY in JavaScript

We will learn how to validate a date formatted as DD/MM/YYYY in JavaScript with two methods: Using split(), reverse() and join() and using moment.js. This basic knowledge is very useful when you work with dates in Javascript.

Validate a date formatted as DD/MM/YYYY in JavaScript 

Using split(), reverse() and join()

The first function works on a string object, the remaining two functions operate on an array.

Syntax:

split(separator)
reverse()
join(separator)

Parameters:

  • separator: The slash (/) character to separate each part of the date, such as date, month and year.

You can use those functions to convert a string into an array of three elements of date (day, month, and year respectively), then reverse the array’s elements and join it into a string of the correct format of a date.

let string = '30/10/2022';
let revString = string.split('/').reverse().join('/');
console.log(revString);

let dateObj = new Date(revString);
console.log(dateObj.toLocaleDateString());

Output: 

2022/10/30
10/30/2022

The above example points out that the original string given is in DD/MM/YYYY format. However this format cannot be used to create a date object as the Date() constructor only accepts the date formatted as YYYY/MM/DD, which is the reverse pattern of the original format. So our idea is to split the original string into an array that contains the values [‘DD’, ‘MM’, ‘YYYY’] and then reverse the array, resulting in [‘YYYY’, ‘MM’, ‘DD’]. Finally, merge it into a string with a slash separator to result in a string formatted as YYYY/MM/DD. However, if the date value you provided is not valid, the Date() object cannot be created, and the result would be like this: 

let string = '99/10/2022';
let revString = string.split('/').reverse().join('/');
console.log(revString);

let dateObj = new Date(revString);
console.log(dateObj.toLocaleDateString());

Output: 

2022/10/99
Invalid Date

As a result, you can check its validity by comparing it to the string “Invalid Date”, for example:

let string = '99/10/2022';
let revString = string.split('/').reverse().join('/');
console.log(revString);
let dateObj = new Date(revString);
let dateString = dateObj.toLocaleDateString();

if (dateString == 'Invalid Date') {
	console.log('The Date is not valid, please try another');
}

Output: 

2022/10/99
The Date is not valid, please try another

Using moment.js

With this method, you have to import the library at the script tag of your HTML document.

<script src="https://momentjs.com/downloads/moment.js"></script>

Now you may easily use this library to convert a string to a date and validate formatted as DD/MM/YYYY by using the moment() method:

Syntax:

moment(string, format)

Parameters:

  • string: This parameter is a string that represents a date format as DD/MM/YYYY
  • format: The string represents the format of the outdated string. In our case, it is DD/MM/YYYY

This function accepts a date string and a string that defines the format and returns the date.

let string = "95/10/2022";
let stringToMoment = moment(string, "DD/MM/YYYY");
console.log(stringToMoment.isValid());

Output:

false

The example above uses the isValid() method of the moment object to check whether the date provided is a valid date or not. If it is a valid date, you can print it like any Date object. 

let string = "5/10/2022";
let stringToMoment = moment(string, "DD/MM/YYYY");
console.log(stringToMoment.isValid());
console.log(stringToMoment.format())

Output: 

true
2022-10-05T00:00:00+07:00

Summary

We have learned how to validate a date formatted as DD/MM/YYYY in JavaScript. It would be necessary to remember that each approach has its pros and cons. You can validate the date and format by using our methods in this tutorial and other ones. Thanks for reading!

Maybe you are interested:

Leave a Reply

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