JS中Date与String的相互转换

1- Date -> String
new Date().toLocaleDateString();  ==> 2017/4/15

2- Date -> "2017-04-15"
                 var year = new Date().getFullYear(); //获取完整的年份(4位,1970-????)
                var month = new Date().getMonth() + 1; //获取当前月份(0-11,0代表1月)
                var day = new Date().getDate(); //获取当前日(1-31)
                if (month < 10) {
                    month = "0" + month;
                }
                if (day < 10) {
                    day = "0" + day;
                }
var dateString =  year + "-" + month + "-" + day;
==> 2017-04-15

3- JsonDate -> Date
     var  date   new Date(parseInt(jsonDate.replace( "/Date(" , "" ).replace( ")/" , "" ), 10));

你可能感兴趣的:(JQuery)