3

I have a string which is in the format "05-01-2016" when i run the below code in chrome i get the correct output

var fromDate = new Date(searchOpts.RunDateFrom);

        fromDate.format("yyyy-MM-dd");

output = "2016/05/01"

However, when this code is execute inside my js file i 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

2 Answers2

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

If you just need the string

amulous
  • 642
  • 2
  • 6
  • 15
1
var year = fromDate.getFullYear();
var month = fromDate.getMonth() < 10 ? '0'+ fromDate.getMonth()+1 : fromDate.getMonth()+1

var date =  fromDate.getDate() < 10 ? '0'+ fromDate.getDate(): fromDate.getDate()
Deepak Garg
  • 142
  • 6