Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Java by (13.1k points)

Can anyone help me with the date.parse method, it is giving a wrong output in this condition:

new Date(Date.parse("2005-07-08"));

Output:

Thu Jul 07 2005 17:00:00 GMT-0700 (PST)

Any help would be appreciated.

1 Answer

0 votes
by (26.7k points)

You can use the below format to get the right result:

// parse a date in yyyy-mm-dd format

function parseDate(input) {

  let parts = input.split('-');

  // new Date(year, month [, day [, hours[, minutes[, seconds[, ms]]]]])

  return new Date(parts[0], parts[1]-1, parts[2]); // Note: months are 0-based

}

Basically, with the newer edition, this date.parse returns as a number rather than as a date.

I hope this will help.

Want to become a Java Expert? Join Java Course now!!

Want to know more about Java? Watch this tutorial on Java Course | Java Tutorial for Beginners:

Related questions

0 votes
1 answer
asked Apr 4, 2021 in Java by dante07 (13.1k points)
0 votes
1 answer
0 votes
1 answer
asked Mar 15, 2021 in Java by dante07 (13.1k points)
0 votes
1 answer
asked Jan 28, 2021 in Java by dante07 (13.1k points)
0 votes
1 answer
asked Dec 17, 2020 in SQL by Appu (6.1k points)

Browse Categories

...