admin管理员组文章数量:1426066
I need to count total no of months between the two dates in months and years if not just months either any php snippet or ether js or jquery.
I display the data from backend using php tags {{ $job->job_start_date }} job_start_date is for start date and {{ $job->job_expiry_date }}) job_expiry_date is for end date.
I get my output as in this format 13-Oct-2016. I tried this thing putting the values as hidden but couldn't get it work with parsing properly while adding new Date() to them
$(document).ready(function(){
var sDate = $("#monthStart").val();
var nDate = $("#monthEnd").val();
var sd = new Date(sDate );
var ed = new Date(nDate );
ed.setDate(ed.getDate() - sd.getDate());
alert(monthDiff(sd,ed));
});
function monthDiff(d1, d2) {
var months;
months = (d2.getFullYear() - d1.getFullYear()) * 12;
months -= d1.getMonth() + 1;
months += d2.getMonth();
return months <= 0 ? 0 : months;
}
need a easy solution in js jquery or php.
I need to count total no of months between the two dates in months and years if not just months either any php snippet or ether js or jquery.
I display the data from backend using php tags {{ $job->job_start_date }} job_start_date is for start date and {{ $job->job_expiry_date }}) job_expiry_date is for end date.
I get my output as in this format 13-Oct-2016. I tried this thing putting the values as hidden but couldn't get it work with parsing properly while adding new Date() to them
$(document).ready(function(){
var sDate = $("#monthStart").val();
var nDate = $("#monthEnd").val();
var sd = new Date(sDate );
var ed = new Date(nDate );
ed.setDate(ed.getDate() - sd.getDate());
alert(monthDiff(sd,ed));
});
function monthDiff(d1, d2) {
var months;
months = (d2.getFullYear() - d1.getFullYear()) * 12;
months -= d1.getMonth() + 1;
months += d2.getMonth();
return months <= 0 ? 0 : months;
}
need a easy solution in js jquery or php.
Share Improve this question edited Dec 3, 2016 at 11:31 Pavan Tolety asked Oct 5, 2016 at 4:59 Pavan ToletyPavan Tolety 1632 silver badges16 bronze badges 3- i have created in angularjs hop it will help you. codepen.io/halimmln/pen/NRPBBK – halim Commented Oct 5, 2016 at 5:06
- It is not clear how you define the number of months. For example, is difference between 30th/Sep and 1st/Oct going to be 1 month or 0 months? In general, when working with date and time it is a good idea to use some library, like momentjs just because of all the corner cases you may end-up handling. – Vladimir M Commented Oct 5, 2016 at 5:25
- I need to count it as 28 days – Pavan Tolety Commented Oct 5, 2016 at 9:39
4 Answers
Reset to default 2function MonthDiff(date1, date2) {
var Nomonths;
Nomonths= (date2.getFullYear() - date1.getFullYear()) * 12;
Nomonths-= date1.getMonth() + 1;
Nomonths+= date2.getMonth() +1; // we should add + 1 to get correct month number
return Nomonths <= 0 ? 0 : Nomonths;
}
function differenceInMonths($startDate, $endDate)
{
$date1 = new DateTime($startDate);
$date2 = new DateTime($endDate);
$interval = date_diff($date1, $date2);
return $interval->m + ($interval->y * 12) . ' months';
}
This way you get no. of months. Now you can calculate years if months >= 11.
Well in this case you can take advantage of moment.js
library.
Assuming you have Date String
in format like "1-Sept-2016" then we need to convert it to date and then apply moment. If you already have Date
then you can directly apply the moment
.
Demo :
var startDate = new Date('01-Sept-2016' );
var endDate = new Date('30-Oct-2016' );
var endMoment = moment(endDate);
var startMoment = moment(startDate);
//[days, years, months, seconds, ...]
console.log(endMoment.diff(startMoment, 'months'));
<script src="https://ajax.googleapis./ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare./ajax/libs/moment.js/2.15.1/moment.js"></script>
You can even specify the difference you want like days
, months
,years
, seconds
as specified above.
Reference : http://momentjs./
Here is a php solution:
$noMonth = 1 + (date('Y',strtotime($job->job_expiry_date)) - date('Y',strtotime($job->job_start_date))) * 12 + (date('m',strtotime($job->job_expiry_date)) - date('m',strtotime($job->job_start_date)))
The first part calculates the difference in years, multiplied by 12 results the number of month:
1 + (date('Y',strtotime($job->job_expiry_date)) - date('Y',strtotime($job->job_start_date))) * 12
The second part calculates the number of months to be added or subtracted, as if the dates were in the same year:
(date('m',strtotime($job->job_expiry_date)) - date('m',strtotime($job->job_start_date)))
本文标签: javascriptNeed to calculate months between two dates in JQueryStack Overflow
版权声明:本文标题:javascript - Need to calculate months between two dates in JQuery - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745467409a2659592.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论