在javascript中将字符串转换为datetime对象

在javascript中将字符串转换为datetime对象,javascript,Javascript,我必须将当前日期的日期对象从javascript发送到后端 我正在做的是 var currentDate = new Date(); var dateString = currentDate.getMonth() + "-" + currentDate.getDate() + "-" + currentDate.getFullYear() + " " + currentDate.getHours() + ":" + currentDate.getMinutes() + ":" + c

我必须将当前日期的日期对象从javascript发送到后端

我正在做的是

var currentDate = new Date();
var dateString = currentDate.getMonth() + "-"
  + currentDate.getDate() + "-" + currentDate.getFullYear() + " "
  + currentDate.getHours() + ":" + currentDate.getMinutes() + ":"
  + currentDate.getSeconds();
var newDate = new Date(Date.parse(dateString));
但它是说newDate的日期无效

我必须将2013年10月3日下午6:10:25作为datetime对象发送到后端

var currentDate = new Date(),
    utcYear = currentDate.getUTCFullYear(),
    utcMonth = ('0' + currenctDate.getUTCMonth()).slice(-2),
    utcDay = ('0' + currentDate.getUTCDate()).slice(-2),
    fullDateString = utcMonth.toString() + '/' + utcDay.toString() + '/' + utcYear.toString();
如果你想得到时间部分,同样的原则。

不要在月/日和日/年之间加-,只要加空格就行了

var currentDate = new Date(),
    dateString = currentDate.getMonth() + " " + 
                  currentDate.getDate() + " " + 
              currentDate.getFullYear() + " " + 
                 currentDate.getHours() + ":" + 
               currentDate.getMinutes() + ":" + 
               currentDate.getSeconds(),
    newDate = new Date(dateString);

console.log(newDate)

为什么你不直接使用setYear、setMonth等。对我来说很好->@adeneo Firefox 19.0.2和20.0,OSX会给出错误这在什么浏览器中不起作用?顺便说一句,Date.parsecurrentDate.toString将始终有效当您说backend时,您是说您正在尝试将日期发送回服务器吗?您需要时间戳格式吗?您应该在getDate之后添加逗号,这是dateString的正确格式:new Date1995年12月17日03:24:00;,参见@JanTuroň有很多方法可以插入数字并获得正确的日期。您链接的文档至少提供了我一眼就能看到的3个。基于此,您可以使用月号,副月名。您不必在日期后包含逗号。事实上,如果浏览器遵循RFC 2822规范,则有很多可能性: