Using the following:
使用以下:
var timestart = $('.thisDiv').data("timestart");
var startDateTime = new Date(timestart);
to collect a date from a php file that is updating by ajax from this:
要从ajax从以下文件更新的php文件中收集日期:
$TimeStart = date( 'Y,m,d,g,i', $TimeStart );
<div class="thisDiv" data-timestart="<?= $TimeStart ?>"></div>
var timestart = $('.thisDiv').data("timestart");
In console I'm getting the following when logging timestart
and startDateTime
:
在控制台,当记录timestart和startDateTime时,我得到以下信息:
2017,07,24,7,50
Invalid Date
If I paste the date that is output as follows
如果我粘贴输出的日期如下所示
var startDateTime = new Date(2017,07,24,7,50);
Then it works fine. Any ideas why I'm getting Invalid Date?
然后它将正常工作。你知道为什么我的约会无效吗?
2 个解决方案
#1
2
Your timestart
variable (JavaScript) is just a string. So it's a string 2017,07,24,7,50
, and not those elements in order - which can't be used as separate parameters like new Date()
expects.
您的timestart变量(JavaScript)只是一个字符串。因此,它是一个字符串2017、07、24、7、50,而不是那些按顺序排列的元素——不能像new Date()所期望的那样作为单独的参数使用。
Let's take a look at it!
让我们看一看!
var startDateTime = new Date(2017,07,24,7,50); // Parameters in order - all OK!
var startDateTime = new Date("2017,07,24,7,50"); // A single string - single parameter, not OK!
You need to return a proper format of dates from PHP with a format that's valid in JavaScript. Per the ECMAScript standard, the valid format that should work across all browsers is YYYY-MM-DDTHH:mm:ss.sssZ
(see the reference at the bottom). To define that from PHP, you would need to format it as such
您需要从PHP返回一个合适的日期格式,该格式在JavaScript中是有效的。根据ECMAScript标准,应该在所有浏览器上工作的有效格式是YYYY-MM-DDTHH:mm:ss。sssZ(见下面的参考)。要从PHP中定义它,需要将其格式化
$TimeStart = date('c', $TimeStart);
This would return a format such as 2017-07-24T21:08:32+02:00
.
这将返回一个格式,例如:2017-07-24T21:08:32+02:00。
Alternatively, you can use a splat/spread-operator ...
and split the string it into elements, which I find as the better approach than above.
或者,您可以使用splat/spread-operator…将字符串拆分为元素,我认为这比上面的方法更好。
var timestart = $('.thisDiv').data("timestart"); // Get the string: "2017,07,24,7,50"
timestart = timestart.split(","); // Split into array
var startDateTime = new Date(...timestart); // Pass as arguments with splat-operator
- Spread/splat operator in JavaScript
- 传播/ JavaScript长条木板运营商
- PHP
date()
documentation - PHP日期()的文档
- ECMAScript date string (JavaScript)
- ECMAScript日期字符串(JavaScript)
#2
0
You need to convert your date from string format to numbers.
您需要将日期从字符串格式转换为数字。
var timestart = $('.thisDiv').data("timestart").split(",").map(Number);
var startDateTime = new Date(...timestart);
console.log(startDateTime)
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="thisDiv" data-timestart="2017,07,24,7,50"></div>
#1
2
Your timestart
variable (JavaScript) is just a string. So it's a string 2017,07,24,7,50
, and not those elements in order - which can't be used as separate parameters like new Date()
expects.
您的timestart变量(JavaScript)只是一个字符串。因此,它是一个字符串2017、07、24、7、50,而不是那些按顺序排列的元素——不能像new Date()所期望的那样作为单独的参数使用。
Let's take a look at it!
让我们看一看!
var startDateTime = new Date(2017,07,24,7,50); // Parameters in order - all OK!
var startDateTime = new Date("2017,07,24,7,50"); // A single string - single parameter, not OK!
You need to return a proper format of dates from PHP with a format that's valid in JavaScript. Per the ECMAScript standard, the valid format that should work across all browsers is YYYY-MM-DDTHH:mm:ss.sssZ
(see the reference at the bottom). To define that from PHP, you would need to format it as such
您需要从PHP返回一个合适的日期格式,该格式在JavaScript中是有效的。根据ECMAScript标准,应该在所有浏览器上工作的有效格式是YYYY-MM-DDTHH:mm:ss。sssZ(见下面的参考)。要从PHP中定义它,需要将其格式化
$TimeStart = date('c', $TimeStart);
This would return a format such as 2017-07-24T21:08:32+02:00
.
这将返回一个格式,例如:2017-07-24T21:08:32+02:00。
Alternatively, you can use a splat/spread-operator ...
and split the string it into elements, which I find as the better approach than above.
或者,您可以使用splat/spread-operator…将字符串拆分为元素,我认为这比上面的方法更好。
var timestart = $('.thisDiv').data("timestart"); // Get the string: "2017,07,24,7,50"
timestart = timestart.split(","); // Split into array
var startDateTime = new Date(...timestart); // Pass as arguments with splat-operator
- Spread/splat operator in JavaScript
- 传播/ JavaScript长条木板运营商
- PHP
date()
documentation - PHP日期()的文档
- ECMAScript date string (JavaScript)
- ECMAScript日期字符串(JavaScript)
#2
0
You need to convert your date from string format to numbers.
您需要将日期从字符串格式转换为数字。
var timestart = $('.thisDiv').data("timestart").split(",").map(Number);
var startDateTime = new Date(...timestart);
console.log(startDateTime)
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="thisDiv" data-timestart="2017,07,24,7,50"></div>