New Date()返回无效日期 [英] New Date() returns invalid date

查看:318
本文介绍了New Date()返回无效日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用jQuery计算两天之间的差异。输入字段是Bootstrap日期触发器。

I'm trying to calculate a difference between 2 days using jQuery. The input fields are the Bootstrap datepicker ones.

当我在console.log的字段值时,他们给我的日期格式为dd-mm-yyyy

When I console.log the field values, they give me a date an in the format dd-mm-yyyy

代码

console.log($("#actie_begin").val());

日志


27/06/2016

27/06/2016

然而,当我尝试使用 >新日期() (做计算),变量变为无效日期

However when I try to use a new date() (to do the calculations) on it, the variable becomes 'Invalid date'

代码: / strong>

Code:

var start_date = new Date($("#actie_begin").val());

日志


无效日期

Invalid Date

我该如何解决?

推荐答案

您使用的格式不支持 Date.parse

您可以提取日期部分并调用日期(年,月,日)构造函数

The format you use is not supported by Date.parse.
You could extract the date parts and call the Date(year, month, day) constructor

var starts = $("#actie_begin").val();
var match = /(\d+)\/(\d+)\/(\d+)/.exec(dates)
var start_date = new Date(match[3], match[2], match[1]);

这篇关于New Date()返回无效日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆