Forum Moderators: open

Message Too Old, No Replies

need js to display future date/day

...three days from now

         

coachm

7:28 pm on Dec 1, 2008 (gmt 0)

10+ Year Member Top Contributors Of The Month



I need a js (or actually a flash actionscript would probably do) that displays the date and day three days from now within a sentence, such as:

You will recieve your widget on [day, date].

I'm javascript ignorant, so I'm basically looking for something I can cut and post.

Anyone know where there might be such a script, or have one they could post?

I've searched around, but the things I have found were only snippets, and I lack the knowledge to get the things working.

lavazza

10:03 pm on Dec 1, 2008 (gmt 0)

10+ Year Member



Hopefully this will help
[pre]<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html lang="en-GB">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>widget delivery date</title>
</head>
<body>
<form name="myFormName" method="get" action=" ">
<h1>Widget Due Date</h1>
<noscript><div>Javascript must be enabled in your browser</div></noscript>
<p>Lorem ipsum dolor sit amet</p>
<script type="text/javascript">
var myDelayInDays=2; // adjust this value to suit
var myDate=new Date();
var oneDayInMilliseconds = (24*60*60*1000);
var myTimeInMillisconds=myDate.getTime()+(myDelayInDays * oneDayInMilliseconds);
myDate.setTime(myTimeInMillisconds);
/*
setTime() is an 'inbuilt' javascript function
that calculates a date and time by adding or subtracting
a specified number of milliseconds to/from midnight January 1, 1970
it can be buggy in some browsers
*/
var widgetDueDate = myDate.toLocaleDateString();
/*
toLocaleDateString() is an 'inbuilt' javascript function
that converts a Date object, according to local time,
to a string and returns the date portion
*/
document.write('<p>You will receive your widget in '
+ myDelayInDays
+ ' days, on <strong>'
+ widgetDueDate + '<\/strong><\/p>');
</script>
</form>
<p>Nam commodo libero nec justo. Sed diam risus, suscipit eu</p>
</body>
</html>[/pre]

coachm

2:58 am on Dec 2, 2008 (gmt 0)

10+ Year Member Top Contributors Of The Month



Big Thank you. Took a while to copy it so I didn't mess up the comments, but once I cleaned it up, it looks like it works like a charm.

If you have a sec, just two questions. Did you write this off the top of your head? (if so, wow).

Also what's the function of the form part?

Thanks again.

lavazza

5:10 am on Dec 2, 2008 (gmt 0)

10+ Year Member



Big Thank you.
You're welcome

Took a while to copy it so I didn't mess up the comments
You can, of course, delete the comments...

But... Yeah... Sorry about the indentation via spacebar-spacing...

For some reason I can't get my head around this forum software and the way it handles (or, maybe, doesn't handle) tab-spacing...

In order to preserve some sort of indentation, I simply 'find-and-replace' my tabs with a bunch of spaces :(

it looks like it works like a charm
Cool. And its valid HTML4- strict, too :)

Did you write this off the top of your head?
almost... I have a few old files that do something very similar

what's the function of the form part?
To be honest, I don't recall if it's (ever) 'required' or just 'recommended practice'...

In this case, on such a simple page, its probably a bit of overkill...

But...

On a more complex page, with 2 or more script sections, it can be handy to be able to address specific elements both by element-id AND by form-name - e.g. if/when you (might) have two elements with the same name (can easily happen if more than one person is writing/maintaining code!)

e.g
document.frmAlpha.selEmpName.selectedIndex
and
document.frmBravo.selEmpName.selectedIndex
address two distinct elements :)

At least... that's what I think the rationale is

Anyhoo... if it works, don't fix it is what I say :)

coachm

1:52 am on Dec 3, 2008 (gmt 0)

10+ Year Member Top Contributors Of The Month



Again, thanks, and thanks for the explanation. Although I don't plan to program, or have a need (usually) it's nice to learn enough to have a basic understanding and to understand basic coding when it's put in front of me.