-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?

13
  • 2
    How is 1635750314812 in any way YYYY/MM/DD?! Commented Nov 1, 2021 at 7:54
  • @deceze took the words out of my mouth Commented Nov 1, 2021 at 7:54
  • @deceze, i don't understand what you mean Commented Nov 1, 2021 at 7:55
  • moment('1635750314812', 'YYYY/MM/DD') means parse 1635750314812 assuming it is in YYYY/MM/DD format. Which it's clearly not. Commented Nov 1, 2021 at 7: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. Commented Nov 1, 2021 at 7:56

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.