繁体   English   中英

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

[英]convert string into datetime object in javascript

我必须将当前日期的Date对象从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 日期无效

我必须将3-10-2013 6:10:25 PM作为日期时间对象发送到后端。

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)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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