Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

How do I get the difference between 2 dates in full days (I don't want any fractions of a day)

(如何获得整天2个日期之间的差额(我不想一天中的任何时间))

var date1 = new Date('7/11/2010');
var date2 = new Date('12/12/2010');
var diffDays = date2.getDate() - date1.getDate(); 
alert(diffDays)

I tried the above but this did not work.

(我尝试了上述方法,但这没有用。)

  ask by chobo2 translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
595 views
Welcome To Ask or Share your Answers For Others

1 Answer

Here is one way :

(这是一种方法 :)

const date1 = new Date('7/13/2010');
const date2 = new Date('12/15/2010');
const diffTime = Math.abs(date2 - date1);
const diffDays = Math.ceil(diffTime / (1000 * 60 * 60 * 24)); 
console.log(diffDays);

Observe that we need to enclose the date in quotes.

(请注意,我们需要将日期用引号引起来。)

The rest of the code gets the time difference in milliseconds and then divides to get the number of days.

(其余代码获得时差(以毫秒为单位),然后除以天数。)

Date expects mm/dd/yyyy format.

(日期应采用mm / dd / yyyy格式。)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...