I have this code:
const date = moment(person.indefiniteContractDate
.toISOString()
.substring(2),
'YY-MM-DD');
if (date.isAfter('2020-08-15'))
I want to ask for the current year, but always for '08-15', how can I do that?
I have this code:
const date = moment(person.indefiniteContractDate
.toISOString()
.substring(2),
'YY-MM-DD');
if (date.isAfter('2020-08-15'))
I want to ask for the current year, but always for '08-15', how can I do that?
The moment
function is capable of accepting a Date
. Just call moment(person.indefiniteContractDate)
. If you need to set the hours/min/sec to zero, do so after passing in the Date
object. There is a function called startOf
that can handle setting the time to zero for the day.
Note: moment is EOL (end of life), you should use Luxon instead.
// IF
{
const person = { indefiniteContractDate: new Date() }
// https://stackoverflow.com/a/19699447/1762224
const date = moment(person.indefiniteContractDate).startOf('day');
if (date.isAfter('2020-08-15')) {
console.log('AFTER!');
}
}
// ELSE
{
const person = { indefiniteContractDate: new Date(2020, 7, 13) }
const date = moment(person.indefiniteContractDate).startOf('day');
if (date.isAfter('2020-08-15')) {
console.log('AFTER!');
} else {
console.log('BEFORE!'); // <-- Here
}
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.29.1/moment.min.js"></script>
You can go by moment('08-15','MM-DD')
This will get you the 08-15 of current year
So I think your code could be
const date = moment(person.indefiniteContractDate
.toISOString()
.substring(2),
'YY-MM-DD');
const compareDate = moment('08-15','MM-DD');
if (date.isAfter(compareDate))