日期比较始终使用类型为date的文本字段返回false

时间:2017-05-24 15:08:28

标签: javascript date

我一直试图比较两个日期。使用getElementById标记从html获取pdate,其中使用js Date()类生成其他标记。每当我转换它们时,它都会一直检查为假。

var whatNow = new Date();
//pdate is value fetched using javascript from type=date textfield
//console.log(pdate) prints (2017-5-25) which is current 
if(pdate >= whatNow.getFullYear()+'-'+(whatNow.getMonth()+1)+'-
    '+whatNow.getDate()){
//selected date is greater than or equals to current date. Good to do code.
else {
//this code runs always no matter what I do.
}

1 个答案:

答案 0 :(得分:1)

您正在比较字符串,这可能会导致错误。比较new AlertDialog.Builder(this) .setTitle("Closing application") .setMessage("Are you sure you want to exit?") .setPositiveButton("Yes", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { } }).setNegativeButton("No", null).show(); 代替:

Date
相关问题