无法使用Javascript将给定日期与当前日期进行比较
我面临一个问题。我需要使用Javascript将给定的日期与今天的日期进行比较。我在下面解释我的代码无法使用Javascript将给定日期与当前日期进行比较,javascript,Javascript,我面临一个问题。我需要使用Javascript将给定的日期与今天的日期进行比较。我在下面解释我的代码 function getCurrentDate(){ var today = new Date(); var dd = today.getDate(); var mm = today.getMonth()+1; var yyyy = today.getFullYear(); if(dd<10) { dd='0'+dd;
function getCurrentDate(){
var today = new Date();
var dd = today.getDate();
var mm = today.getMonth()+1;
var yyyy = today.getFullYear();
if(dd<10)
{
dd='0'+dd;
}
if(mm<10)
{
mm='0'+mm;
}
today = dd+'-'+mm+'-'+yyyy;
return today;
}
var givendate=new Date('19-01-2018');
var todaydate=$scope.getCurrentDate();
if (givendate >= todaydate) {
console.log('bool',true);
}else{
console.log('bool',false);
}
函数getCurrentDate(){
var today=新日期();
var dd=today.getDate();
var mm=today.getMonth()+1;
var yyyy=today.getFullYear();
如果(dd如您在评论
givendate
中所说的是“无效日期”
——这就是为什么您有false
因此,在解析日期之前,请检查
newdate
尝试分析以下内容。引用:
格式如下。此处显示的组件必须完全存在,
正是这个标点符号。注意“T”出现了
字符串中的字面意思,表示时间的开始
元素,如ISO 8601中规定
Year:
YYYY (eg 1997)
Year and month:
YYYY-MM (eg 1997-07)
Complete date:
YYYY-MM-DD (eg 1997-07-16)
Complete date plus hours and minutes:
YYYY-MM-DDThh:mmTZD (eg 1997-07-16T19:20+01:00)
Complete date plus hours, minutes and seconds:
YYYY-MM-DDThh:mm:ssTZD (eg 1997-07-16T19:20:30+01:00)
Complete date plus hours, minutes, seconds and a decimal fraction of a
second
YYYY-MM-DDThh:mm:ss.sTZD (eg 1997-07-16T19:20:30.45+01:00)
where:
YYYY = four-digit year
MM = two-digit month (01=January, etc.)
DD = two-digit day of month (01 through 31)
hh = two digits of hour (00 through 23) (am/pm NOT allowed)
mm = two digits of minute (00 through 59)
ss = two digits of second (00 through 59)
s = one or more digits representing a decimal fraction of a second
TZD = time zone designator (Z or +hh:mm or -hh:mm)
在构建日期对象时,请确保传递了格式正确的日期。有关主题的所有详细信息都可以在JS MDN中找到,如您在注释中所述,在上,givendate是
“无效日期”
-这就是为什么会出现false
因此,在解析日期之前,请检查
newdate
尝试分析以下内容。引用:
格式如下。此处显示的组件必须完全存在,
正是这个标点符号。注意“T”出现了
字符串中的字面意思,表示时间的开始
元素,如ISO 8601中规定
Year:
YYYY (eg 1997)
Year and month:
YYYY-MM (eg 1997-07)
Complete date:
YYYY-MM-DD (eg 1997-07-16)
Complete date plus hours and minutes:
YYYY-MM-DDThh:mmTZD (eg 1997-07-16T19:20+01:00)
Complete date plus hours, minutes and seconds:
YYYY-MM-DDThh:mm:ssTZD (eg 1997-07-16T19:20:30+01:00)
Complete date plus hours, minutes, seconds and a decimal fraction of a
second
YYYY-MM-DDThh:mm:ss.sTZD (eg 1997-07-16T19:20:30.45+01:00)
where:
YYYY = four-digit year
MM = two-digit month (01=January, etc.)
DD = two-digit day of month (01 through 31)
hh = two digits of hour (00 through 23) (am/pm NOT allowed)
mm = two digits of minute (00 through 59)
ss = two digits of second (00 through 59)
s = one or more digits representing a decimal fraction of a second
TZD = time zone designator (Z or +hh:mm or -hh:mm)
在构造日期对象时,请确保传递了格式正确的日期。有关主题的所有详细信息都可以在JS MDN中找到,在上,下面的代码起作用。正如其他人所说,您需要确保按照日期构造函数所期望的格式指定日期
<!DOCTYPE html>
<html>
<body>
<p id="demo"></p>
<p id="today"></p>
<p id="text"></p>
<script>
today = new Date();
jan19 = new Date('2018-01-19')
document.getElementById("demo").innerHTML = jan19;
document.getElementById("today").innerHTML = today;
if (today < jan19) {
document.getElementById("text").innerHTML = 'true';
}
else {
document.getElementById("text").innerHTML = 'false';
}
</script>
</body>
</html>
今天=新日期();
1月19日=新日期('2018-01-19')
document.getElementById(“demo”).innerHTML=jan19;
document.getElementById(“今日”).innerHTML=今日;
如果(今天<1月19日){
document.getElementById(“text”).innerHTML='true';
}
否则{
document.getElementById(“text”).innerHTML='false';
}
下面的代码有效。正如其他代码所说,您需要确保按照日期构造函数所期望的格式指定日期
<!DOCTYPE html>
<html>
<body>
<p id="demo"></p>
<p id="today"></p>
<p id="text"></p>
<script>
today = new Date();
jan19 = new Date('2018-01-19')
document.getElementById("demo").innerHTML = jan19;
document.getElementById("today").innerHTML = today;
if (today < jan19) {
document.getElementById("text").innerHTML = 'true';
}
else {
document.getElementById("text").innerHTML = 'false';
}
</script>
</body>
</html>
今天=新日期();
1月19日=新日期('2018-01-19')
document.getElementById(“demo”).innerHTML=jan19;
document.getElementById(“今日”).innerHTML=今日;
如果(今天<1月19日){
document.getElementById(“text”).innerHTML='true';
}
否则{
document.getElementById(“text”).innerHTML='false';
}
这对我很有用。如果你不想要自定义格式的日期,那么你可以使用它
函数getCurrentDate(){
var today=新日期();
今天回来;
}
var givendate=新日期(“2018-01-19”);
var todaydate=getCurrentDate();
控制台日志(givendate);
console.log(今天);
如果(给定日期>=今天日期){
console.log('bool',true);
}否则{
console.log('bool',false);
}这对我很有用。如果你不想要自定义格式的日期,那么你可以使用它 函数getCurrentDate(){ var today=新日期(); 今天回来; } var givendate=新日期(“2018-01-19”); var todaydate=getCurrentDate(); 控制台日志(givendate); console.log(今天); 如果(给定日期>=今天日期){ console.log('bool',true); }否则{ console.log('bool',false);
}如果要继续使用当前日期格式(
dd-MM-yyyy
),则可能需要将字符串按-
拆分,并相应地重新排列以生成有效日期
JS代码
函数getCurrentDate(){
var today=新日期();
var dd=today.getDate();
var mm=today.getMonth()+1;
var yyyy=today.getFullYear();
如果(dd<10)
dd='0'+dd;
如果(毫米<10)
毫米='0'+毫米;
今天=dd+'-'+mm+'-'+yyyy;
今天回来;
}
var todayDate=getCurrentDate().toString();
var givenDate='19-01-2018';
var todayDateArray=todayDate.split('-');
var givenDateArray=givenDate.split('-');
todayDate=新日期(todayDateArray[2]+'-'
+todayDateArray[1]+'-'
+todayDateArray[0]);
givenDate=新日期(givenDateArray[2]+'-'
+GivenDataArray[1]+'-'
+givendaray[0]);
如果(给定日期>=今天日期)
console.log('bool',true);
其他的
console.log('bool',false);如果要继续使用当前的日期格式(
dd-MM-yyyy
),则可能需要按-
拆分字符串,并相应地重新排列以生成有效日期
JS代码
函数getCurrentDate(){
var today=新日期();
var dd=today.getDate();
var mm=today.getMonth()+1;
var yyyy=today.getFullYear();
如果(dd<10)
dd='0'+dd;
如果(毫米<10)
毫米='0'+毫米;
今天=dd+'-'+mm+'-'+yyyy;
今天回来;
}
var todayDate=getCurrentDate().toString();
var givenDate='19-01-2018';
var todayDateArray=todayDate.split('-');
var givenDateArray=givenDate.split('-');
todayDate=新日期(todayDateArray[2]+'-'
+todayDateArray[1]+'-'
+todayDateArray[0]);
givenDate=新日期(givenDateArray[2]+'-'
+GivenDataArray[1]+'-'
+givendaray[0]);
如果(给定日期>=今天日期)
console.log('bool',true);
其他的
console.log('bool',false);在调试时,也
console.log
givendate
和todaydate