简体   繁体   中英

Javascript 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 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

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

如果你只需要字符串

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()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM