我有以下代码。如果我们尝试在英国服务器设置中检索日,月和年。日期对象返回不正确的值。
var startDate = "30/08/2013";
var d = new Date(startDate);
alert(d.getFullYear()); //2015
请帮帮我
答案 0 :(得分:1)
如果想要更好地控制解析,请使用moment.js:
var startDate = "30/08/2013";
var m = moment(startDate,"DD/MM/YYYY");
alert(m.year()); //2013
答案 1 :(得分:0)
尝试将日期作为参数列表传递:
var startDate = "30/08/2013"; // Would also work with 30-08-2013
var startDateArray;
if(startDate.contains('/'))
startDateArray = startDate.split('/');
else if (startDate.contains('-'))
startDateArray = startDate.split('-');
else if (startDate.contains('.'))
startDateArray = startDate.split('.');
var d = new Date(startDateArray[2], startDateArray[1]-1, startDateArray[0]); // Month is 0 based, that's why we are subtracting 1