By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
432,069 Members | 1,716 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 432,069 IT Pros & Developers. It's quick & easy.

Can Java Script allow me to calculate a future date?

P: n/a
Im a long time java developer and actually have never done anything
with java scripting. I'd like to write a short simple script for
calculating a date in the future based on today's date and a letter.
Can I use javascripting to create a webpage to allow a user to enter a
letter and then click a button to find a future calendar date? I'm just
not sure how much user interaction scripting allows. Does java
scripting allow buttons, textfields and the ability to put enough
smarts in to calculate a future date?

Aug 13 '06 #1
Share this Question
Share on Google+
6 Replies


P: n/a

ro*****@gmail.com написа:
Im a long time java developer and actually have never done anything
with java scripting. I'd like to write a short simple script for
calculating a date in the future based on today's date and a letter.
You can make this easily. See the reference for JavaScript Date object.
http://www.w3schools.com/jsref/jsref_obj_date.asp
This is smal example how to add one day to current date:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html>
<head>
<title></title>
<script type="text/javascript">
var today=new Date();
today.setDate(today.getDate()+1);
document.write("Tomorrow is:"+ today.toLocaleString());
</script>
</head>
<body>
</body>
</html>
Can I use javascripting to create a webpage to allow a user to enter a
letter and then click a button to find a future calendar date? I'm just
not sure how much user interaction scripting allows. Does java
scripting allow buttons, textfields and the ability to put enough
smarts in to calculate a future date?
Yes JavaScript is very usefull for this tasks. Tell me your idea and I
will help you with a
pleasure.
Best Regardss

Aug 13 '06 #2

P: n/a

<ro*****@gmail.comwrote in message
news:11**********************@b28g2000cwb.googlegr oups.com...
Im a long time java developer and actually have never done anything
with java scripting. I'd like to write a short simple script for
calculating a date in the future based on today's date and a letter.
Can I use javascripting to create a webpage to allow a user to enter a
letter and then click a button to find a future calendar date? I'm just
not sure how much user interaction scripting allows. Does java
scripting allow buttons, textfields and the ability to put enough
smarts in to calculate a future date?
In short, yes. JavaScript is a much more powerful, much more feature-rich
language than most people give it credit for.

I'm not sure what you mean by "based on a future date an a letter" but it
can. I assume you mean that each letter would relate to a set timespan to
add to the date? (You can't add a letter to a date and get a date - you
need to add a timespan)

In any case you'll find in JavaScript many ways to do this. You might use a
switch statement based on the form-field value. You might create an object
(think hashmap) to hold your name=value pairs and use them directly with
index notation (as in myObject["myKey"] = myValue). You could have the
value of the form (perhaps a drop-down select box) already contain the
timespan value.

There are many, many ways (in such a rich language) depending on your exact
specification.

As far as adding to the current date (getting the future date) JavaScript's
Date object is very rich. There are "set" and "get" methods for all data
parts and several abstractions.

I've also encapsulated a lot of this into a Date Extensions library which
adds several features to the standard Date object. One of the new methods
is a "add()" method which adds a specified number of a specified timespan to
the date:

http://www.depressedpress.com/Conten...ions/Index.cfm

Timespans allowed are milliseconds, seconds, minutes, quarterhours, warhols,
halfhours, hours, days, weeks, businessdays, businessweeks, wholeweeks,
months and years.

Using it can do, for example, myDate.add(2, "months") and get a date two
months in the future.

The library takes care of many "tricky" aspects of dates like differing
numbers of days in months and leap years. However for non-ambiguious
timespans (minutes, seconds, hours, etc) the calculation itself is just
plain addition.

The code is open-source: feel free to dig through it and see what's going
on.

Jim Davis
Aug 13 '06 #3

P: n/a
JRS: In article <Ht******************************@giganews.com>, dated
Sun, 13 Aug 2006 12:55:45 remote, seen in news:comp.lang.javascript, Jim
Davis <ne********@vboston.composted :
>
As far as adding to the current date (getting the future date) JavaScript's
Date object is very rich. There are "set" and "get" methods for all data
parts and several abstractions.

Actually, there is only one data part, an IEEE Double of milliseconds
from 1970.0 GMT. There are "get" methods for most derivable components
of conventional forms of the UTC/local date/time (but not Week Number,
although ISO 8601 defines it). For most of those, but not all, there is
a "set" method.

--
John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 IE 4
<URL:http://www.jibbering.com/faq/>? JL/RC: FAQ of news:comp.lang.javascript
<URL:http://www.merlyn.demon.co.uk/js-index.htmjscr maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/TP/BP/Delphi/jscr/&c, FAQ items, links.
Aug 13 '06 #4

P: n/a
JRS: In article <11**********************@b28g2000cwb.googlegroups .com>
, dated Sun, 13 Aug 2006 01:00:36 remote, seen in
news:comp.lang.javascript, ro*****@gmail.com posted :
>Im a long time java developer and actually have never done anything
with java scripting.
There is no space in "Javascript".
I'd like to write a short simple script for
calculating a date in the future based on today's date and a letter.
Can I use javascripting to create a webpage to allow a user to enter a
letter and then click a button to find a future calendar date?
Yes, and if you had read the newsgroup FAQ ... See sig.
I'm just
not sure how much user interaction scripting allows. Does java
scripting allow buttons, textfields and the ability to put enough
smarts in to calculate a future date?
See for example <URL:http://www.merlyn.demon.co.uk/js-clndr.htm>.

Javascript goes only up to AD 275760-09-13 with the built-in capability;
but can do up to about AD 3E12 if you code your own.

--
John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 IE 4
<URL:http://www.jibbering.com/faq/>? JL/RC: FAQ of news:comp.lang.javascript
<URL:http://www.merlyn.demon.co.uk/js-index.htmjscr maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/TP/BP/Delphi/jscr/&c, FAQ items, links.
Aug 13 '06 #5

P: n/a
"Dr John Stockton" <jr*@merlyn.demon.co.ukwrote in message
news:Pd**************@merlyn.demon.co.uk...
JRS: In article <Ht******************************@giganews.com>, dated
Sun, 13 Aug 2006 12:55:45 remote, seen in news:comp.lang.javascript, Jim
Davis <ne********@vboston.composted :
>>
As far as adding to the current date (getting the future date)
JavaScript's
Date object is very rich. There are "set" and "get" methods for all data
parts and several abstractions.


Actually, there is only one data part, an IEEE Double of milliseconds
from 1970.0 GMT. There are "get" methods for most derivable components
of conventional forms of the UTC/local date/time (but not Week Number,
although ISO 8601 defines it). For most of those, but not all, there is
a "set" method.
Sorry - typo. I meant "date part".

Although (since you got me thinking) "data part" is, I think, applicable -
not physically as in the memory storage of the implementation but rather (as
you imply) as information derivable from that implementation. It's
applicable since I was talking about the interface to the Date object rather
than the implementation.

One piece of information that the object provides you is "month". It's a
piece of data - a "data part" (although that phrase _is_ cumbersome). How
the object provides this isn't really relevant: it might store it as a hard
coded value, derive it from a formula or tatoo it on a pixie's ear.

The implementation doesn't matter practically - what does matter is that the
data returned (the month in the case) is always the same. It's unambigious
and always available: whether it's stored directly or derived on the fly
it's a part of the data being exposed.

It depends totally on how you define the almost completely ambigious phrase
"data part".

(I find the assumed definitions of ambigious phrases very interesting. Some
people tend to automatically apply strict definitions to undefined phrases.
Others tend to expand interpertation of such phrases until they essentially
become meaningless. Most people, it seems, tend to fall in the middle.
I've often thought that a metric of personality as a quantification of a
person's reaction to ambiguity might have some value.)

Jim Davis

Aug 14 '06 #6

P: n/a
JRS: In article <79******************************@giganews.com>, dated
Mon, 14 Aug 2006 01:20:53 remote, seen in news:comp.lang.javascript, Jim
Davis <ne********@vboston.composted :
>"Dr John Stockton" <jr*@merlyn.demon.co.ukwrote in message
news:Pd**************@merlyn.demon.co.uk...
>JRS: In article <Ht******************************@giganews.com>, dated
Sun, 13 Aug 2006 12:55:45 remote, seen in news:comp.lang.javascript, Jim
Davis <ne********@vboston.composted :
>>>
As far as adding to the current date (getting the future date)
JavaScript's
Date object is very rich. There are "set" and "get" methods for all data
parts and several abstractions.


Actually, there is only one data part, an IEEE Double of milliseconds
from 1970.0 GMT. There are "get" methods for most derivable components
of conventional forms of the UTC/local date/time (but not Week Number,
although ISO 8601 defines it). For most of those, but not all, there is
a "set" method.

Sorry - typo. I meant "date part".

Although (since you got me thinking) "data part" is, I think, applicable -
not physically as in the memory storage of the implementation but rather (as
you imply) as information derivable from that implementation. It's
applicable since I was talking about the interface to the Date object rather
than the implementation.
...
There is a test, ISTM : The Date Object is required to behave as if it
stores just an IEEE Double of milliseconds from 1970.0 GMT, limit 10^8
days-worth; but it is neither required to store only that, not to store
exactly that.

Using only UTC functions, set a date from year, month, day and then read
out year, month, day in that order. We know that converting YMD to
milliseconds and milliseconds to YMD are tasks of similar complexity.
If setting YMD and reading Y take similar times but then reading M & D
is much faster, then there must be some sort of cache for M & D and
presumably Y; if each of the four take a similar time, then probably
only milliseconds are stored. That could be browser-dependent.
I've now put the code for function setDay in my js-date8.htm; also a
proposed method for setting the Week. Corresponding setUTC... functions
are then obvious. But function setTimezoneOffset is probably
impossible.

--
John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 IE 4
<URL:http://www.jibbering.com/faq/>? JL/RC: FAQ of news:comp.lang.javascript
<URL:http://www.merlyn.demon.co.uk/js-index.htmjscr maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/TP/BP/Delphi/jscr/&c, FAQ items, links.
Aug 14 '06 #7

This discussion thread is closed

Replies have been disabled for this discussion.