English 中文(简体)
如何检查投入日期是否等于今天的日期?
原标题:How to check if input date is equal to today s date?

I have a form input with an id of date_trans . The format for that date input (which is validated server side) can be any of:

  • dd/mm/yyyy
  • dd-mm-yyyy
  • yyyy-mm-dd
  • yyyy/mm/dd

然而,在张贴表格之前,我要检查“过渡领域”的日期是否与今天的日期相同。 如果所收日期是客户日期(即使用js),因为我也对服务器进行了双重检查。

I m totally lost on how to do the date comparrison in jQuery or just plain old javascript. If it helps, I am using the jquery datepicker

最佳回答

对纯联合材料进行简单日期比较应当足够:

// Create date from input value
var inputDate = new Date("11/21/2011");

// Get today s date
var todaysDate = new Date();

// call setHours to take the time out of the comparison
if(inputDate.setHours(0,0,0,0) == todaysDate.setHours(0,0,0,0)) {
    // Date equals today s date
}

http://jsfiddle.net/mzdqc/>a working JSFiddle。

问题回答

for completeness, taken from this solution:

你们可以利用:

var today = new Date();
var isToday = (today.toDateString() == otherDate.toDateString());

没有图书馆的附属设施,也没有比以前的答复中显示的“众人”做法更清洁。

使用时间。 j)

moment( dd/mm/yyyy ).isSame(Date.now(),  day );

You can replace day string with year, month, minute if you want.

function sameDay( d1, d2 ){
  return d1.getUTCFullYear() == d2.getUTCFullYear() &&
         d1.getUTCMonth() == d2.getUTCMonth() &&
         d1.getUTCDate() == d2.getUTCDate();
}

if (sameDay( new Date(userString), new Date)){
  // ...
}

利用UTC*方法,确保不同时区与同一全球日相匹配的两天时间相同。 (如果你重新规定这两个日期,则没有必要,但需要考虑的是一件好事)。

Just use the following code in your javaScript:

if(new Date(hireDate).getTime() > new Date().getTime())
{
//Date greater than today s date 
}

根据你的要求改变条件。 http://www.w3schools.com/js/js_comparisons.asp” rel=“noreferer” 相比之下,java script

Try this

// method to check date is less than today date
  isLessDate(schedule_date : any){
    var _schedule_date = new Date(schedule_date);
    var date = new Date();
    var transformDate = this.datePipe.transform(date,  yyyy-MM-dd );
    var _today_date = new Date(  +transformDate);
    if(_schedule_date < _today_date){
      return  small 
    }
    else if(_schedule_date > _today_date){
      return  big 
    }
    else {
      return  same 
    }
  }

Date.js是一个手提图书馆,用于操纵和编排日期。 它可以在这种情况下提供帮助。

比较日期的最佳方式和建议方式是:

var today = new Date().getTime();
var reqDateVar = new Date(somedate).getTime();

if(today === reqDateVar){
 // NOW
} else {
 // Some other time
}




相关问题
selected text in iframe

How to get a selected text inside a iframe. I my page i m having a iframe which is editable true. So how can i get the selected text in that iframe.

How to fire event handlers on the link using javascript

I would like to click a link in my page using javascript. I would like to Fire event handlers on the link without navigating. How can this be done? This has to work both in firefox and Internet ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Clipboard access using Javascript - sans Flash?

Is there a reliable way to access the client machine s clipboard using Javascript? I continue to run into permissions issues when attempting to do this. How does Google Docs do this? Do they use ...

javascript debugging question

I have a large javascript which I didn t write but I need to use it and I m slowely going trough it trying to figure out what does it do and how, I m using alert to print out what it does but now I ...

Parsing date like twitter

I ve made a little forum and I want parse the date on newest posts like twitter, you know "posted 40 minutes ago ","posted 1 hour ago"... What s the best way ? Thanx.

热门标签