-1

var beginningTime = moment('1635750314812', 'YYYY/MM/DD'); // Today date
var endTime = moment(undefined, 'YYYY/MM/DD') // Today date
console.log(beginningTime.isSame(endTime)); //expect true
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.29.1/moment.min.js"></script>

I am using moment js to get if 2 date are the same using the above pattern. But now i get false instead of true. How to solve the issue?

Asking
  • 3,487
  • 11
  • 51
  • 106
  • 2
    How is 1635750314812 in any way YYYY/MM/DD?! – deceze Nov 01 '21 at 07:54
  • @deceze took the words out of my mouth – eamanola Nov 01 '21 at 07:54
  • @deceze, i don't understand what you mean – Asking Nov 01 '21 at 07:55
  • `moment('1635750314812', 'YYYY/MM/DD')` means *parse `1635750314812` assuming it is in `YYYY/MM/DD` format*. Which it's clearly not. – deceze Nov 01 '21 at 07:55
  • `'YYYY/MM/DD'` is supposed to inform Moment about the format of the first argument. `moment('1635750314812', 'YYYY/MM/DD')` means : "Hey Moment, I'm informing you that `'1635750314812'` is in format `'YYYY/MM/DD')`. Moment is like 'Wat?' and produces an invalid Moment object. Log it, it says `_isValid:false`. – Jeremy Thille Nov 01 '21 at 07:56
  • @deceze, how to change the code, could you help please? how to compare these 2 days? – Asking Nov 01 '21 at 07:56
  • Perhaps look at this post: https://stackoverflow.com/questions/11847806/javascript-date-set-just-the-date-ignoring-time. If you insist on using moment.js: moment().format("YYYY/MM/DD") should be used. – AgentM Nov 01 '21 at 07:58
  • @deceze, created something like this `const test = moment(moment('1635750314812').format('YYYY/MM/DD')).isSame(moment(undefined).format('YYYY/MM/DD')) console.log(test);` , but anyway i get false. – Asking Nov 01 '21 at 08:01
  • @AgentM, created something like this const test = moment(moment('1635750314812').format('YYYY/MM/DD')).isSame(moment(undefined).format('YYYY/MM/DD')) console.log(test); , but anyway i get false. – Asking Nov 01 '21 at 08:05
  • `moment(moment())'`? `moment(undefined)` ? Do you feel any of this is right? – Jeremy Thille Nov 01 '21 at 08:07
  • @JeremyThille, `console.log(moment(undefined).format('YYYY/MM/DD')); ` returns today's date. What do you mean? – Asking Nov 01 '21 at 08:11
  • But why do you add `undefined`? `moment()` returns today's date. Also why `moment( moment() )`? – Jeremy Thille Nov 01 '21 at 08:13
  • I think they intend to create an object containing only the `Date` information from a `timestamp` and try to see if that `Date` is equal to the date of `now`. You could do something like `moment(moment(1635750314812).format('YYYY/MM/DD')).isSame(moment(moment().format('YYYY/MM/DD'))`, but it quickly gets quite ugly. There are probably better ways to achieve it. – AgentM Nov 01 '21 at 08:20

1 Answers1

2

Instead of formatting the beginningTime and endTime separately, you should take a look at the 'granularity' argument for isSame().

I made a few adjustments to your code for it to work, and also look cleaner.

var beginningTime = moment(1635750314812); // Today date (from timestamp)
var endTime = moment() // Today date
console.log(beginningTime.isSame(endTime, 'day')); //expect true
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.29.1/moment.min.js"></script>

It first simply creates a moment of your timestamp and a moment of right now, whenever now is. Then it will compare the two moments with a granularity of a day, effectively checking if the day and everything bigger (month, year) is the same.

AgentM
  • 406
  • 4
  • 18