0

I have a code like this where I have my date and current/today date and I need to get difference in days between two dates. I always get 0 or some big number "31536000000" Whats wrong?

const startDate = moment(job.published_date, 'YYYY-MM-DD').format('YYYY,MM,DD')  //looks like this "2018,08,19"
const endDate = moment(new Date()).format('YYYY,MM,DD') //looks like this "2019,09,12"
const a = moment([ startDate, ])   
const b = moment([ endDate, ])
console.log('DIFFERENCE IN DAYS', b.diff(a))
Igor-Vuk
  • 3,551
  • 7
  • 30
  • 65
  • what is the exact value of `job.published_date` – Dan Sep 12 '19 at 16:53
  • it is a string that looks like this for example 2017-08-16 – Igor-Vuk Sep 12 '19 at 16:54
  • Does Moment *know* what format `startDate` and `endDate` are in? Because you are trying to parse them from one known format into another, then parse them into Moment objects again and this might fail on the second step. – VLAZ Sep 12 '19 at 16:55
  • not sure if it knows. Do I need to add something somewhere. job.published_date is a string that looks like this for example 2017-08-16 and for endDate I just get new Date and format it like the startDate – Igor-Vuk Sep 12 '19 at 16:58

3 Answers3

4
const startDate = moment('2017-08-16', 'YYYY-MM-DD').format('YYYY,MM,DD')  //looks like this "2018,08,19"
const endDate = moment(new Date()).format('YYYY,MM,DD') //looks like this "2019,09,12"
const a = moment([ startDate, ])   
const b = moment([ endDate, ])
console.log('DIFFERENCE IN DAYS', moment.duration(b.diff(a)).asDays());
// DIFFERENCE IN DAYS 730

you need to get a duration value from moment after the diff - then format it as days.

this helped

Get hours difference between two dates in Moment Js

Dan
  • 3,755
  • 4
  • 27
  • 38
  • 1
    `console.log('DIFFERENCE IN DAYS', b.diff(a, 'days'));` also returns 730 – dikuw Sep 12 '19 at 17:01
  • yea that may be better Michael – Dan Sep 12 '19 at 17:02
  • Ok. Seems something is wrong with using a variable. If I use "2017-08-16" in startDate I get correct. But if I use variable `job.published_date` I get 0. Don't know why since I checked it, date is here before function is run and it is a string. – Igor-Vuk Sep 12 '19 at 17:10
  • your `job` object is not what you think it is , you may have a race condition where your function is ran before your variable is set – Dan Sep 12 '19 at 17:23
  • I checked that. It's not. job is fetched long before this function is run. It's weird. The khodadadi solution below worked for me. – Igor-Vuk Sep 12 '19 at 17:36
  • the fact that his solution works when its wrapped in 2 different functions - leads me to believe that you are indeed running into a race condition... glad we could help you out though cheers. – Dan Sep 12 '19 at 19:37
2
dayCount(startDate,endDate){
  const startDateMoment = moment(startDate, 'YYYY-MM-DD').unix();
  const endDateMoment = moment(endDate,"YYYY-MM-DD").unix();

  const dayCount = (endDateMoment-startDateMoment)/86400;

  //or 
  //const diff = endDateMoment-startDateMoment;
  //const dayCount = moment.duration(diff,'s').asDays()

  return dayCount
}

render() {
  let dayCount= this.dayCount("2018,08,19","2019,09,12");
  console.log(dayCount)
}
Javad Khodadadi
  • 410
  • 1
  • 4
  • 13
  • your solution worked for me but not sure why the one at the top didn't. Instead of using string at the date at the startdate I used variable. When I use variable that contains string value it didn't work. – Igor-Vuk Sep 12 '19 at 21:15
2

Looking at the documentation it looks like you want b.diff(a, 'days'). Without days it's printing the difference in milliseconds https://momentjs.com/docs/#/displaying/difference/

Maria
  • 295
  • 2
  • 11