/    Sign up×
Community /Pin to ProfileBookmark

Hi I’ve been working on what should be a simple script I want to take a date and compare it with the current date and if it is in the past it is invalid.

Here’s my code right now the date is hard coded but I would like to change it to be a variable. Does anyone see anything wrong with my code sometimes I think it’s working and then it isn’t.

<script>
today = new Date();
enteredDate = new Date(“23/12/2004”);

if (today < enteredDate){

alert(“this date has past”);

}
else{
alert(“this is a good date”);
}

</script>

TIA

Nikki

to post a comment
JavaScript

8 Comments(s)

Copy linkTweet thisAlerts:
@TheBearMayJan 07.2004 — As you've constructed it enteredDate returns "Sat Nov 12 00:00 EST 2005" try using :

enteredDate = new Date("December 23, 2004");
Copy linkTweet thisAlerts:
@nbenton99authorJan 07.2004 — thanks but my problem then is that the users must enter the date as DD/MM/YYYY how can I do a compare with that?

TIA

Nikki
Copy linkTweet thisAlerts:
@TheBearMayJan 07.2004 — Not a problem:

[code=php]
var dVal="25/12/2004";

var monArr=["Jan","Feb","Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"]
var dValDay = dVal.substring(0,2);
var dValMnth = dVal.substring(3,5);
var dValYr = dVal.substring(6,10);
var enteredDate2=monArr[dValMnth-1]+ " " + dValDay + ", " + dValYr;
enteredDate = new Date(enteredDate2);[/code]
Copy linkTweet thisAlerts:
@nbenton99authorJan 07.2004 — Is this right?

<script>




today = new Date();


//enteredDate = new Date("23/12/2004");

var dVal.value="25/12/2004";

var monArr=["Jan","Feb","Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"];

var dValDay = dVal.substring(0,2);

var dValMnth = dVal.substring(3,5);

var dValYr = dVal.substring(6,10);

var enteredDate2=monArr[dValMnth-1]+ " " + dValDay + ", " + dValYr;

enteredDate = new Date(enteredDate2);



if (today < enteredDate){

alert("this date has past");

}

else{

alert("this is a good date");

}

</script>


I'm still getting an error on the page.

TIA

Nikki
Copy linkTweet thisAlerts:
@TheBearMayJan 07.2004 — Ooops... My bad, I started to change the function after I tested it and didn't reset one of the changes. Remove the ".value" after dVal

[SIZE=1]Changed above also[/SIZE]
Copy linkTweet thisAlerts:
@nbenton99authorJan 07.2004 — Thank you, it's working great now. I'm still trying to understand you code though, why the array and all the substrings. I'm assuming it's altering the format of the date.

Thanks

Nikki
Copy linkTweet thisAlerts:
@TheBearMayJan 07.2004 — I'm just picking apart (the substrings) the current date format and coverting it to a string in the form "Mmm dd, yyyy". The array was the easiest way I could think of to pull in the month as a text value quickly. Since it's a zero based array the value we want is 1 less than the month entered, i.e. monArr[0]=Jan. Thus enteredDate2 = "Dec 25, 2004"
Copy linkTweet thisAlerts:
@nbenton99authorJan 07.2004 — I see. Thank you for your help it is working beautifully now.


Thanks

Nikki
×

Success!

Help @nbenton99 spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.6,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...