javascriptdatedatetimesolr

Convert string to date format yyyy-MM-dd


I have a string which is in the format "05-01-2016". When I run the below code directly in the Chrome console, I get the correct output:

var fromDate = new Date(searchOpts.RunDateFrom);
fromDate.format("yyyy-MM-dd");

Output is "2016/05/01"

However, when this code is executed inside my JS file, I instead get this output:

Sun May 01 2016 00:00:00 GMT+0530 (India Standard Time)

How do I prevent this? I need to pass the date in this format "2016-05-01" to solr


Solution

  • formattedDate = fromDate.getFullYear() + "-" + (fromDate.getMonth()+1) + "-" + fromDate.getDate()
    

    If you just need the string