/    Sign up×
Community /Pin to ProfileBookmark

how to convert strings to dates

Hello ?

I am capturing a month and year from two combo boxes in a form (cardMonth and cardYear). I concatenate them to get 12/2004 for example. Then I want to compare it agains today, 4/2004. However it keeps evaluating to false. I have to convert the strings to dates.

How? Do I use some type of date format function?
In VBA there is a CDate function….is there something similar in Javascript that will convert a string to a date?

Here is my code:

function formValidate()
{
var now = new Date();
var creditExp;
var checkNow;
creditExp=document.OrderEntry.cardMonth.value + “/” + document.OrderEntry.cardYear.value;
checkNow=now.getMonth()+1 + “/” + now.getYear();

if (creditExp>=now.getDate())
{
alert(“Placed order”);
}else{
alert(“This credit card has expired.”);
}

}

Any guidance is greatly appreciated – thanks!
Heather

to post a comment
JavaScript

14 Comments(s)

Copy linkTweet thisAlerts:
@SomatoseApr 27.2004 — hporto,

I'd reccomend comparing the strings as YYYYMM instead of MM/YYYY. For example, instead of 12/2004, you'd have 200412. And instead of 4/2004 you'd have 200404.

You'll need to make sure you add a zero to any single digit months if it's not already doing so. After all, you wouldn't want to compare 20044 to 200412.

-Matt
Copy linkTweet thisAlerts:
@SomatoseApr 27.2004 — hporto, try this:

[code=php]
function addZero(strMonth) {
if (strMonth.length == 1) { strMonth = '0' + strMonth; }
return (strMonth);
}

function formValidate() {
var now = new Date();
var creditExp = document.OrderEntry.cardYear.value + addZero(document.OrderEntry.cardMonth.value);
var checkNow = now.getYear() + addZero(now.getMonth());

if (creditExp >= checkNow) {
alert("Placed order");
}
else {
alert("This credit card has expired.");
}
}
[/code]


BTW, I noticed you were building the checkNow variable, but not using it.

-Matt
Copy linkTweet thisAlerts:
@SomatoseApr 27.2004 — Oh yeah, and I almost forgot to mention. I _think_ that using "var now = new Date();" will get the date that's on the _client's_ machine (but you might want to check on that).

Now, while this will normally be the correct date, it might not be. And since you're using the date to determine if the user's credit card has expired or not, you'll probably want to use the date on your server instead of the client's machine. Else the user might set his system date back to when his card was still valid and fool your script.

Try using a server side include (or something similar) to get the current date on your server.

-Matt
Copy linkTweet thisAlerts:
@hportoauthorApr 27.2004 — Thank you all for your help! I really appreciate your prompt replies!!!!!

Somatose, I liked your advice on using YYYYMM instead of MM/YYYY. Plus, I also appreciate your comments regarding new Date() and how it takes the date from the user's computer/system date.

Again, thank you all!

Here is a function that I used to make the comparison:

<script language="JavaScript">

function formValidate()

{

if (document.OrderEntry.cardMonth.value > now.getMonth()+1 ||

document.OrderEntry.cardYear.value > now.getYear())

{

alert("Placed order");

}else{

alert("This credit card has expired.");

}

}
Copy linkTweet thisAlerts:
@SomatoseApr 28.2004 — hporto,

Are you sure that last script you posted is what you want to use?

Only comparing the months OR the years would mean a card set to expire in 10/2001 would still be accepted today (since the month of expiration, October, is after the current month of April).

I think it would be best if you went back to compairing the strings (in the YYYYMM format).

Just a thought.

?
Copy linkTweet thisAlerts:
@hportoauthorApr 28.2004 — Thanks for picking that up!!!!!

Heather ?
Copy linkTweet thisAlerts:
@hportoauthorApr 28.2004 — Hi Somatose,

This line of code is not working right:

var checkNow = now.getYear() + addZero(now.getMonth()+1);

Intead of returning 200404

it is returning 2004+04 which equals 2008.

How do I get it to concentate instead of add?

Thanks!!!

Heather ?
Copy linkTweet thisAlerts:
@SomatoseApr 28.2004 — I'm sure there's a better way to do this, but it's not coming to me right now...

Anyway, try this addZero function instead of the other one:

function addZero(strMonth) {

strMonth = strMonth + '';

if (strMonth.length == 1) { strMonth = '0' + strMonth; }

return (strMonth);

}
Copy linkTweet thisAlerts:
@hportoauthorApr 28.2004 — Hello again ?

That function worked! Thank you ?

I knew it should have to do with a null string, but I wasn't sure where to put it.

Thanks so much for you help!!!!

Heather
Copy linkTweet thisAlerts:
@SomatoseApr 28.2004 — Indeed, here is the better way.

I was using the + '' to do an impromptu type conversion from number to string. The proper way to do the type conversion though is by using String().

So try this instead, (it'll look better :p)

function addZero(strMonth) {

strMonth = String(strMonth);

if (strMonth.length == 1) { strMonth = '0' + strMonth; }

return (strMonth);

}

If you want, you could also covert now.getYear() to a string when you're building the checkNow variable, as such:

var checkNow = String(now.getYear()) + addZero(now.getMonth()+1);

But it shouldn't be necessary to do so since the addZero function should be returning a string anyhow.
Copy linkTweet thisAlerts:
@SomatoseApr 28.2004 — And just to beat a dead horse ...

You could also use:

var checkNow = String(now.getYear()) + (String(now.getMonth()+1).length == 1 ? ('0' + String(now.getMonth()+1)) : String(now.getMonth()+1));

instead of using the addZero function.
Copy linkTweet thisAlerts:
@SomatoseApr 28.2004 — *Sigh* Still got more "horse" left ...

Here's something funcitonally the same but a little bit shorter:

var checkNow = String(now.getYear()) + (String(now.getMonth()+1).length == 1 ? '0' : '') + String(now.getMonth()+1);
Copy linkTweet thisAlerts:
@hportoauthorApr 28.2004 — Hello again ?

Thank you so very much for all your time and help with the problem/question.

Heather
×

Success!

Help @hporto 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.28,
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,
)...