function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Lightning PracticeLightning Practice 

how to find difference between two dates in lightning?

Hi all,
how to find difference between two dates and need to get every mondays between two dates?
can any one help me out.

var startD=component.find('startDate').get('v.value'); 
        
        var lastD=component.find('lastDate').get('v.value');
       
        var diff= endDa - startDa;
getting NAN value?

Thanks ,
Ram.
Best Answer chosen by Lightning Practice
Ashif KhanAshif Khan
Hi Ram,
var date1 = new Date("4/03/2018");//mm/dd/yyyy
var date2 = new Date("6/03/2018");//mm/dd/yyyy
var timeDiffrence = Math.abs(date2.getTime() - date1.getTime());
var differDays = Math.ceil(timeDiffrence / (1000 * 3600 * 24)); 
var AllMondayDate=[];
for(i=0;i<=differDays; i++){
var dateStart = new Date("4/03/2018");
dateStart.setDate(dateStart.getDate() + i);
if(dateStart.getDay()==1){
AllMondayDate.push((dateStart.getMonth() + 1)+'/'+dateStart.getDate()+'/'+dateStart.getFullYear());
}
}
console.log(AllMondayDate);

I hope this will help you If it works mark as solved
Regards
Ashif

All Answers

Ashif KhanAshif Khan
Hi

try it I hope it will help you 
var date1 = new Date("2/01/2012");//mm/dd/yyyy
var date2 = new Date("3/01/2012");//mm/dd/yyyy
var timeDiffrence = Math.abs(date2.getTime() - date1.getTime());
var differDays = Math.ceil(timeDiffrence / (1000 * 3600 * 24)); 
alert(differDays );
Regards
Ashif
Lightning PracticeLightning Practice
Thanks Ashif it works and i need to get all mondays between date 1 and date 2 ?
Thanks,
Ram.
Ashif KhanAshif Khan
Hi Ram

do you need date of All Monday or the total number of Monday
Lightning PracticeLightning Practice
Hi Ashif Khan,

I need dates of All mondays not count.
example 
April 3 2018  to June 3 2018
I need all monday dates     9 16 23 30 7 14 21 28 

Thanks ,
Ram
Ashif KhanAshif Khan
Hi Ram,
var date1 = new Date("4/03/2018");//mm/dd/yyyy
var date2 = new Date("6/03/2018");//mm/dd/yyyy
var timeDiffrence = Math.abs(date2.getTime() - date1.getTime());
var differDays = Math.ceil(timeDiffrence / (1000 * 3600 * 24)); 
var AllMondayDate=[];
for(i=0;i<=differDays; i++){
var dateStart = new Date("4/03/2018");
dateStart.setDate(dateStart.getDate() + i);
if(dateStart.getDay()==1){
AllMondayDate.push((dateStart.getMonth() + 1)+'/'+dateStart.getDate()+'/'+dateStart.getFullYear());
}
}
console.log(AllMondayDate);

I hope this will help you If it works mark as solved
Regards
Ashif
This was selected as the best answer