JavaScript 来自日期时间选择器的无效日期
JavaScript invalid date from DateTime Picker
我有一个函数可以使用 Bootstrap DateTime Picker 的输入结果创建两个日期,我需要比较这两个日期。
但我的第一个值 (rtime
) 总是给出无效日期。我哪里错了?
注意:stime
不可编辑,用户仅对 rtime
字段使用日期时间选择器。
var stime = '28/11/2017 09:18:52';
var rtime = '04/12/2017 10:16:34';
var lastReturn = new Date(rtime);
var lastOut = new Date(stime);
if (lastReturn >= lastOut) {
console.log("This date is after than the other!");
}
console.log(rtime);
console.log(stime);
console.log(lastReturn);
console.log(lastOut);
此结果表明 LastOut
是无效日期:
格式必须是month/day/year,不是day/month/year
这就是为什么您在 28/11/2017 得到无效日期 09:18:52 因为没有第 28 个月。
console.log(new Date('28/11/2017 09:18:52'))
console.log(new Date('11/28/2017 09:18:52'))
您选择的日期可能是 Month/Day/Year。但您的预计日期是 Day/Month/Year。例如,日期 04/12/2017 将返回 4 月 12 日而不是 12 月 4 日。您可以在字符串上使用正则表达式来替换并以正确的格式制作它。
请检查此代码段:
var stime = "28/11/2017 09:18:52" //$("#movesend").val();
var rtime = "04/12/2017 10:16:34" //$("#moveretorna").val();
function parseDate(dateString) {
return dateString.replace( /(\d{2})\/(\d{2})\/(\d{4})/, "//");
}
var lastReturn= new Date(parseDate(rtime));
var lastOut= new Date(parseDate(stime));
if(lastReturn>= lastOut)
{
console.log("This date is after than the other!");
}
console.log(stime);
console.log(rtime);
console.log(lastReturn);
console.log(lastOut);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
您需要将日期从 DD/MM/YYYY HH:MM:SS
格式转换为 MM/DD/YYYY HH:MM:SS
格式。
转换可以使用string#replace
.
var stime = '28/11/2017 09:18:52';
var rtime = '04/12/2017 10:16:34';
stime = stime.replace(/(..)\/(..)\/(.*)/, '//');
rtime = rtime.replace(/(..)\/(..)\/(.*)/, '//');
var lastReturn = new Date(rtime);
var lastOut = new Date(stime);
if (lastReturn >= lastOut) {
console.log("This date is after than the other!");
}
console.log(rtime);
console.log(stime);
console.log(lastReturn);
console.log(lastOut);
我有一个函数可以使用 Bootstrap DateTime Picker 的输入结果创建两个日期,我需要比较这两个日期。
但我的第一个值 (rtime
) 总是给出无效日期。我哪里错了?
注意:stime
不可编辑,用户仅对 rtime
字段使用日期时间选择器。
var stime = '28/11/2017 09:18:52';
var rtime = '04/12/2017 10:16:34';
var lastReturn = new Date(rtime);
var lastOut = new Date(stime);
if (lastReturn >= lastOut) {
console.log("This date is after than the other!");
}
console.log(rtime);
console.log(stime);
console.log(lastReturn);
console.log(lastOut);
此结果表明 LastOut
是无效日期:
格式必须是month/day/year,不是day/month/year
这就是为什么您在 28/11/2017 得到无效日期 09:18:52 因为没有第 28 个月。
console.log(new Date('28/11/2017 09:18:52'))
console.log(new Date('11/28/2017 09:18:52'))
您选择的日期可能是 Month/Day/Year。但您的预计日期是 Day/Month/Year。例如,日期 04/12/2017 将返回 4 月 12 日而不是 12 月 4 日。您可以在字符串上使用正则表达式来替换并以正确的格式制作它。
请检查此代码段:
var stime = "28/11/2017 09:18:52" //$("#movesend").val();
var rtime = "04/12/2017 10:16:34" //$("#moveretorna").val();
function parseDate(dateString) {
return dateString.replace( /(\d{2})\/(\d{2})\/(\d{4})/, "//");
}
var lastReturn= new Date(parseDate(rtime));
var lastOut= new Date(parseDate(stime));
if(lastReturn>= lastOut)
{
console.log("This date is after than the other!");
}
console.log(stime);
console.log(rtime);
console.log(lastReturn);
console.log(lastOut);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
您需要将日期从 DD/MM/YYYY HH:MM:SS
格式转换为 MM/DD/YYYY HH:MM:SS
格式。
转换可以使用string#replace
.
var stime = '28/11/2017 09:18:52';
var rtime = '04/12/2017 10:16:34';
stime = stime.replace(/(..)\/(..)\/(.*)/, '//');
rtime = rtime.replace(/(..)\/(..)\/(.*)/, '//');
var lastReturn = new Date(rtime);
var lastOut = new Date(stime);
if (lastReturn >= lastOut) {
console.log("This date is after than the other!");
}
console.log(rtime);
console.log(stime);
console.log(lastReturn);
console.log(lastOut);