Skip to content
Advertisement

Extract only the number of the day of the date and compare it with the current one

I need to extract only the day number of a user’s registration date.

And extract only the day number of the current date.

Simply in an if loop, say if the day number the user registered is equal to the day number of the current date, do this, or do that.

Code:

JavaScript

Result:

JavaScript

Why in the first case if 18 = 18 gives me false?

However, if I change the date of the user’s registration and therefore the first 18, from 2020/11/18 to 2020/12/18, then the current month gives me yes!

I need that regardless of the month, just by checking the day if it is the same, tell me yes, where am I wrong?

Advertisement

Answer

You are comparing timestamps, which are measured in seconds. What you are doing is effectively comparing two different points in time, not the days of the month.

You really should be using DateTime. If you want to compare only the day part then you can do something like this.

JavaScript
User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement