0

Lets say there are two textboxes, one to enter in a date and the other to enter in time. Below is an example:

<p><strong>Date:</strong> <input type="text" id="datetxt"></p>

Example of how date is displayed: 25-05-1995

<p><strong>Time:</strong> <input type="text" id="timetxt"></p>

Example of how time is displayed: 14:25

Can someone suggest a way in javascript to compare values of the date and time from the textboxes to the current date and time so if the current date and time is past the date and time entered in the textboxes, then it should display and alert?

2 Answers 2

1

Try this

var dateParts = document.getElementById("datetxt").value.split("-");
var timeParts = document.getElementById("timetxt").value.split(":");

var valueDate = new Date(dateParts[2], (dateParts[1] - 1) ,dateParts[0], timeParts[0], timeParts[1]);

if( (new Date).getTime() > valueDate .getTime() )
{
   alert("passed");
}

Live example: http://jsfiddle.net/TJEMr/

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

1 Comment

Both answers are great. Thank you. I have gone for this one as it does it for all date and times. Thank you again
0

You need to massage your date string into a compatible date format:

    //datetxt textbox value, split on dashes
var date = "25-05-1995".split("-"), 
    //timetxt textbox value
    time = "14:25",
    //put it into format: YYYY-MM-DDThh:mm and creates a date object from it
    dateObj = new Date(date[2] + '-' + date[1] + '-' + date[0] + 'T' + time);

//if today is greater than we have passed that DateTime
if(new Date() > dateObj) {
    alert("After entered date");
} else {
    alert("Not passed yet!");
}

Working example: jsFiddle

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.