各位高手大侠,为什么用js 计算出来的1980-02-01到1980-01-01相差的天数只有29天?

解决方案 »

  1.   

    var d1 = "1980/02/01"
    var d2 ="1980/01/01"
    var s = new Date(d1)-new Date(d2)
    alert(s/1000/3600/24)
    结果是31
      

  2.   

    估计是在月份设置上出错,JS中使用0-11代表1-12月份:
    var d = new Date();
    //undefined
    d
    //Sat Jul 02 2011 14:55:05 GMT+0800 (China Standard Time)
    d.setMonth(7);  //如果你这样设置月份数值,就可能跟预期的不一样的,setMonth(7)实际上是设置月份值为8月
    //1312268005714
    d
    //Tue Aug 02 2011 14:55:05 GMT+0800 (China Standard Time)
      

  3.   

    为什么这样: alert((new Date("1980","02","01").getTime()-new Date("1980","01","01").getTime())/(1000*60*60*24));
    得到的结果是29?
      

  4.   

    Constructor
    new Date(year, month, day, hours, minutes, seconds, ms)
    Arguments
    milliseconds
    The number of milliseconds between the desired date and midnight on January 1, 1970
    (UTC). For example, passing the argument 5000 creates a date that represents five seconds
    past midnight on 1/1/70.
    datestring
    A single argument that specifies the date and, optionally, the time as a string. The string
    should be in a format accepted by Date.parse().
    year
    The year, in four-digit format. For example, specify 2001 for the year 2001. For compatibility
    with early implementations of JavaScript, if this argument is between 0 and 99,
    1900 is added to it.
    month
    The month, specified as an integer from 0 (January) to 11 (December).
    day
    The day of the month, specified as an integer from 1 to 31. Note that this argument uses
    1 as its lowest value, while other arguments use 0 as their lowest value. Optional.
    hours
    The hour, specified as an integer from 0 (midnight) to 23 (11 p.m.). Optional.
    minutes
    The minutes in the hour, specified as an integer from 0 to 59. Optional.