Warning: session_start(): open(/tmp/sess_35259509497498c35229dc01dfaaccc2, 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 Convert A DD/MM/YYYY String To A Date In JavaScript? - LearnShareIT

How To Convert A DD/MM/YYYY String To A Date In JavaScript?

Convert a dd/mm/yyyy string to a Date in JavaScript

As you learn about the Date class in JavaScript, you may not know how to convert a dd/mm/yyyy string to a Date in JavaScript. You can learn how to with the document below.

Convert a dd/mm/yyyy string to a Date in JavaScript

Don’t use Date.parse()

Do not use Date.parse(), since it only returns the specified date in milliseconds.

Using the Date() constructor

The Date() constructor will take either a string value or 3 number or string values indicating year, month and day respectively. Though both have some things to keep note of.

Using Date(string value)

This constructor will take in a string value that must follow the ISO 8601 calendar date extended format ("YYYY-MM-DD"), so it is required that the date string is converted to yyyy-mm-dd format.

The best way to convert dd/mm/yyyy format to yyyy-mm-dd is by using the .split() function, after that either use .reverse() then .join("-") or preferably use destructuring to assign a variable to each value of the date string. 

For context:

String.split(str separator) : Splits a String by the separator into an array of sub-strings.

Example:

const str = "a b c";
const arr = str.split(" ");
console.log(arr);

Output:

["a","b","c"]

Array.reverse(): Reverses the order of an array.

Example:

const arr = ["a","b","c"];
console.log(arr.reverse());

Output:

["c","b","a"]

Array.join(str seperator): Joins every value of an array into a String which is separated by the separator.

Example:

const arr = ["a","b","c"];
const str = arr.join("-");
console.log(str);

Output:

"a-b-c"

Destructuring: Unpacking values of an array or object into distinct variables.

Example:

const [a,b] = ["abc","def"];
console.log(a);
console.log(b);

Output:

"abc"
"def"

Method 1: Using .reverse().join("-")

const date_string = "21/01/2006"; // LMThong's birthday
const date_array = date_string.split("/");
const new_date_string = date_array.reverse().join("-");
const date = new Date(new_date_string);
console.log(date.toDateString());

Output

Sat Jan 21 2006

Method 2: Destructuring

const date_string = "21/01/2006";
const [month,day,year] = date_string.split("/");
const new_date_string = `${year}-${day}-${month}`;
const date = new Date(new_date_string);
console.log(date.toDateString());

Output

Sat Jan 21 2006

Also, note how this uses Template Literals (`) which allows you to insert JavaScript snippets into strings.

Using Date(str/int year, str/int month, str/int day)

Also, an option, though if you choose to use this, please use the Destructuring method to assign the date values to variables. Additionally, the month argument ranges from 0-11 and not 1-12, so it’s required to use the parseInt() function on the month variable.

Code

const date_string = "21/01/2006";
const [day,month,year] = date_string.split("/");
const date = new Date(year, parseInt(month) - 1, day);
console.log(date.toDateString());

Output

Sat Jan 21 2006

Summary

To convert a dd/mm/yyyy string to a Date in JavaScript, you need to use the Date class’ constructor. You can learn more about the Date class here.

Maybe you are interested:

Leave a Reply

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