当心时区
使用日期对象立即表示刚好日期会让您陷入一个巨大的过度精度问题。您需要管理时间和时区以将它们拒之门外,而且它们可以在任何步骤潜入。这个问题的公认答案落入了陷阱。
javascript 日期没有时区的概念。这是一个及时的时刻(自纪元以来的滴答声),带有方便的(静态)函数用于在字符串之间进行转换,默认情况下使用设备的“本地”时区,或者,如果指定,UTC 或其他时区。要使用日期对象表示 just-a-date™,您希望您的日期表示相关日期开始时的 UTC 午夜。这是一个常见且必要的约定,可让您处理日期,而不管其创建的季节或时区。因此,无论是在创建午夜 UTC 日期对象时还是在序列化它时,都需要非常警惕地管理时区的概念。
许多人对控制台的默认行为感到困惑。如果您向控制台喷洒日期,您看到的输出将包括您的时区。这只是因为控制台会呼叫toString()您的约会对象,并toString()为您提供本地代表。基础日期没有时区!(只要时间与时区偏移量匹配,您仍然有一个午夜 UTC 日期对象)
反序列化(或创建午夜 UTC 日期对象)
这是四舍五入的步骤,技巧是有两个“正确”的答案。大多数情况下,您会希望您的日期反映用户的本地时区。我在这里的日期是什么时候。. 新西兰和美国的用户可以同时点击,通常会得到不同的日期。在这种情况下,这样做...
// create a date (utc midnight) reflecting the value of myDate and the environment's timezone offset.
new Date(Date.UTC(myDate.getFullYear(),myDate.getMonth(), myDate.getDate()));
有时,国际可比性胜过当地的准确性。在这种情况下,这样做...
// the date in London of a moment in time. Device timezone is ignored.
new Date(Date.UTC(myDate.getUTCFullYear(), myDate.getUTCMonth(), myDate.getUTCDate()));
反序列化日期
电汇上的日期通常采用 YYYY-MM-DD 格式。要反序列化它们,请执行以下操作...
var midnightUTCDate = new Date( dateString + 'T00:00:00Z');
序列化
在创建时注意管理时区,现在您需要确保在转换回字符串表示时将时区排除在外。这样您就可以安全地使用...
toISOString() 
getUTCxxx() 
getTime() //returns a number with no time or timezone. 
.toLocaleDateString("fr",{timeZone:"UTC"}) // whatever locale you want, but ALWAYS UTC. 
并完全避免其他一切,尤其是...
getYear(), getMonth(),getDate() 
所以回答你的问题,晚了7年......
<input type="date" onchange="isInPast(event)">
<script>
var isInPast = function(event){
  var userEntered = new Date(event.target.valueAsNumber); // valueAsNumber has no time or timezone!
  var now = new Date();
  var today = new Date(Date.UTC(now.getUTCFullYear(), now.getUTCMonth(), now.getUTCDate() ));
  if(userEntered.getTime() < today.getTime())
    alert("date is past");
  else if(userEntered.getTime() == today.getTime())
    alert("date is today");
  else
    alert("date is future");
}
</script>
看到它运行...
2019 年更新……免费的东西……
鉴于此答案的受欢迎程度,我已将其全部放入代码中。以下函数返回一个包装的日期对象,并且只公开那些可以安全地与 just-a-date™ 一起使用的函数。
使用 Date 对象调用它,它将解析为 JustADate 反映用户的时区。用字符串调用它:如果字符串是指定时区的 ISO 8601,我们将把时间部分四舍五入。如果未指定时区,我们会将其转换为反映本地时区的日期,就像日期对象一样。
function JustADate(initDate){
  var utcMidnightDateObj = null
  // if no date supplied, use Now.
  if(!initDate)
    initDate = new Date();
  // if initDate specifies a timezone offset, or is already UTC, just keep the date part, reflecting the date _in that timezone_
  if(typeof initDate === "string" && initDate.match(/((\+|-)\d{2}:\d{2}|Z)$/gm)){  
     utcMidnightDateObj = new Date( initDate.substring(0,10) + 'T00:00:00Z');
  } else {
    // if init date is not already a date object, feed it to the date constructor.
    if(!(initDate instanceof Date))
      initDate = new Date(initDate);
      // Vital Step! Strip time part. Create UTC midnight dateObj according to local timezone.
      utcMidnightDateObj = new Date(Date.UTC(initDate.getFullYear(),initDate.getMonth(), initDate.getDate()));
  }
  return {
    toISOString:()=>utcMidnightDateObj.toISOString(),
    getUTCDate:()=>utcMidnightDateObj.getUTCDate(),
    getUTCDay:()=>utcMidnightDateObj.getUTCDay(),
    getUTCFullYear:()=>utcMidnightDateObj.getUTCFullYear(),
    getUTCMonth:()=>utcMidnightDateObj.getUTCMonth(),
    setUTCDate:(arg)=>utcMidnightDateObj.setUTCDate(arg),
    setUTCFullYear:(arg)=>utcMidnightDateObj.setUTCFullYear(arg),
    setUTCMonth:(arg)=>utcMidnightDateObj.setUTCMonth(arg),
    addDays:(days)=>{
      utcMidnightDateObj.setUTCDate(utcMidnightDateObj.getUTCDate + days)
    },
    toString:()=>utcMidnightDateObj.toString(),
    toLocaleDateString:(locale,options)=>{
      options = options || {};
      options.timeZone = "UTC";
      locale = locale || "en-EN";
      return utcMidnightDateObj.toLocaleDateString(locale,options)
    }
  }
}
// if initDate already has a timezone, we'll just use the date part directly
console.log(JustADate('1963-11-22T12:30:00-06:00').toLocaleDateString())