Home >Web Front-end >JS Tutorial >Example analysis of calculating the month difference between two dates using js

Example analysis of calculating the month difference between two dates using js

coldplay.xixi
coldplay.xixiforward
2020-06-12 18:16:373102browse

Example analysis of calculating the month difference between two dates using jsHow to calculate the month difference between two dates in js?

How many months is the difference between the two times?

//两个日期
var date1 = '2013-03-26';
var date2 = '2011-01-10';
// 拆分年月日
date1 = date1.split('-');
// 得到月数
date1 = parseInt(date1[0]) * 12 + parseInt(date1[1]);
// 拆分年月日
date2 = date2.split('-');
// 得到月数
date2 = parseInt(date2[0]) * 12 + parseInt(date2[1]);
var m = Math.abs(date1 - date2);
alert(m);

Recommended tutorial: "JS Tutorial"

The above is the detailed content of Example analysis of calculating the month difference between two dates using js. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:liqingbo.cn. If there is any infringement, please contact admin@php.cn delete