/    Sign up×
Community /Pin to ProfileBookmark

If statement with dates

Hello everyone! I am 100% completely new to JavaScript. I am trying to complete some JavaScript for an intro JavaScript class.

In a nutshell: I am displaying the date my fiance and I got engaged, am utilizing a button to reveal our wedding date, and would like to display a message if the current date is past the set wedding date. [B]The problem I am having is with displaying a message if the current date is past the wedding date.[/B]

I tested it out by changing the set wedding date to be in the past (I just changed the year to be 2009 temporarily) but the message appeared on the page. I tried playing around with it more, searching for more info using if statements for dates, so on and so forth. I am just coding myself in circles at this point and feel totally lost (hence my username!). What am I doing wrong? How do I make this work? ?

Unfortunately I do not have a test page as I do not have a website yet. Hoping the code is simple enough that posting it below will suffice. Thanks in advance for any help!

[code]
<!DOCTYPE HTML>
<head>
<meta charset=”utf-8″>
<title>Wedding Date Reveal</title>
<script>
<!–

function weddingDate()
{
var wd = new Date();
wd.setFullYear(2013,0,5);

var reveal = document.getElementById(“weddingdate_reveal”);
reveal.innerHTML=wd;
}

function afterWedding()
{
var aw = new Date();

if (aw>wd) {
var afterWedding = document.getElementById(“after_wedding”);write(“Sorry, our wedding date has passed! It was on January 5, 2013.”);
afterWedding.innerHTML=aw;
}
}

//–>
</script>
</head>

<body>

<p>We got engaged on
<script type=”application/javascript”>
var ed = new Date();
ed.setFullYear(2010,10,24);
document.write(ed)
</script>

</p>
<p>We are getting married on … <br />
<button onclick=”weddingDate()”>Click for the date!</button> </p>
<p id=”weddingdate_reveal”></p>

<p id=”after_wedding”></p>

</body>
</html>
[/code]

to post a comment
JavaScript

1 Comments(s)

Copy linkTweet thisAlerts:
@JMRKERApr 10.2012 — Slight re-write...
<i>
</i>&lt;!DOCTYPE HTML&gt;
&lt;head&gt;
&lt;meta charset="utf-8"&gt;
&lt;title&gt;Wedding Date Reveal&lt;/title&gt;
&lt;script type="text/javascript"&gt;
var ed = new Date(2010,10,24);
var wd = new Date(2013,0,5); // change year value for testing purposes
var now = new Date();
function weddingDate() {
document.getElementById('weddingdate_reveal').innerHTML = wd.toDateString();
}
function weddingDate() {
var msg = wd.toDateString();
if (now &gt; wd) {
msg = 'Sorry, our wedding date has passed! It was on '+msg;
}
document.getElementById('weddingdate_reveal').innerHTML = msg;
// alert(now + 'n' + wd); // for testing purposes
}
window.onload = function() {
document.getElementById('engagedOn').innerHTML = 'We got engaged on '+ed.toDateString();
}
&lt;/script&gt;
&lt;/head&gt;
&lt;body&gt;
&lt;p id="engagedOn"&gt;&lt;/p&gt;
&lt;p&gt;We are getting married on ... &lt;br /&gt;
&lt;button onclick="weddingDate()"&gt;Click for the date!&lt;/button&gt; &lt;/p&gt;
&lt;p id="weddingdate_reveal"&gt;&lt;/p&gt;
&lt;/body&gt;
&lt;/html&gt;

You can fancy it up further with some CSS changes

or substitute a different function for the 'toDateString()'
×

Success!

Help @lolwtfamidoing 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.18,
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,
)...