Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

j_watson's avatar

Compare date now and user date input.

I'm trying to compare if 2 dates are equal by converting them to string, but whether they are equal or not it is returning the same results. Are there any fixes or alternatives?

This is my code.

	var current = new Date();
    var month = current.getUTCMonth() + 1;
    var day = current.getUTCDate();
    var year = current.getUTCFullYear();
    var dateToday = year + "-" + month + "-" + day;
    //User's select date
    var dateSelected=$('#date').val();

    var sDateToday = dateToday.toString();
    var sDateSelected = dateSelected.toString();
0 likes
6 replies
tykus's avatar

What value are you getting for dateSelected?

j_watson's avatar

I am getting the complete date in y-m-d format.

tykus's avatar

It might be an issue of a leading 0 on single digit day/month numbers?

	var current = new Date();
    var dateSelected=new Date($('#date').val());

	var sameDay = dateSelected.getUTCMonth() == current.getUTCMonth()
		&& dateSelected.getUTCDate() == current.getUTCDate() 
		&& dateSelected.getUTCFullYear() == current.getUTCFullYear() 
1 like
j_watson's avatar
j_watson
OP
Best Answer
Level 2

Thanks for the reps. I was able to solve it with this

function compareDates() {
            var date = .getElementById('date').value;
              
            var inpDate = new Date(date);
            var currDate = new Date();
              
            if(inpDate.setHours(0, 0, 0, 0) == 
                    currDate.setHours(0, 0, 0, 0))
            {
                console.log('Equal');
            }

Please or to participate in this conversation.