473,372 Members | 1,457 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,372 software developers and data experts.

Date calculator, add or subtract days to or from a given date

Here is the code so far

<form name="thisform">

<h3>Enter time to add to or subtract from:</h3>
(If not entered, current time will be used)<br>

Day: <input name="d1" alt="Day of month" size=3>
Month: <input name="m1" alt="Month" size=3>
Year: <input name="y1" alt="Year" size=5> (4 digits for year, e.g.
1950)<br>
Hours: <input name="h1" alt="Hours" size=3>

Minutes: <input name="i1" alt="Minutes" size=3>
Seconds: <input name="s1" alt="Seconds" size=3>
<h3>Enter time to add/subtract</h3>
<select name="type">
<option value="add">Add
<option value="sub">Subtract
</select>
<input alt="Amount to add/subtract" name=amount size=10>
<select name="unit">
<option value="d">days
<option value="h">hours
<option value="m">minutes
<option value="s">seconds
</select>

(Note: Value to add or subtract must currently be less than 300
years)<br>
<input type=submit alt="Calculate" value="Calculate new date!">
</form>

I would like to click on the submit button and then some kind of alert
window would pop up. Or just write it on the page.

thanks.

Jul 23 '05 #1
8 25340
Lee
dl*****@yahoo.com said:

Here is the code so far I would like to click on the submit button and then some kind of alert
window would pop up. Or just write it on the page.


What class is this for?

Jul 23 '05 #2
It's not for a class,

Here's the Original Code, I am trying to change to use in a more useful
form for me, and ebay too.

<form>
<input type="text" name="date1" size="20"><br>
date (yyyy/mm/dd)<br><br>
<input type="text" name="date2" size="20" readonly><br>
90 days from date<br><br>
<input type="button" value="Calculate"
onclick="plus90(this);">
</form>

<script type="text/javascript">

document.forms(0).date1.value = dateToString(new Date())

function dateToString(d){
return [d.getYear(),d.getMonth()+1,d.getDate()].join('/')

}

function plus90(x){
d = new Date(x.form.date1.value)
d = new Date(d.getYear(),d.getMonth(),d.getDate()+90)
x.form.date2.value = dateToString(d)

}

</script>

Jul 23 '05 #3
Samir wrote:
[...]
<form>
<form action="">

[...]
document.forms(0).date1.value = dateToString(new Date())
document.forms[0].date1.value = dateToString(new Date())
--------------^-^

<URL:http://www.jibbering.com/faq/faq_notes/square_brackets.html>

function dateToString(d){
return [d.getYear(),d.getMonth()+1,d.getDate()].join('/')
getYear() will return the year from 1900 (unless you are using
IE, which does not properly implement getYear). You should
either use getFullYear() or for wider support, create your own
full year function:

return [
myFullYear(d.getYear()),
d.getMonth()+1,
d.getDate()
].join('/')

function myFullYear(y){
return (y<1900)?y+1900:y;
}
}

function plus90(x){
d = new Date(x.form.date1.value)
d = new Date(d.getYear(),d.getMonth(),d.getDate()+90)


Why generate an entirely new date?

function plus90(x){
d = new Date(x.form.date1.value)
d.setDate(d.getDate()+90);

[...]

given the above, subtracting days should be simple easy. As for
time, you should convert the change in hours, minutes and
seconds to milliseconds, then use:

var changeInMilliseconds = 1000 * (
hoursEntered *60*60
+ minutesEntered *60
+ secondsEntered * 1);

d.setTime(d.getTime() + changeInMilliseconds);
secondsEntered needs to be converted from a string to number,
hence *1, which is a nice way of doing it in sync with the other
variables.

Using milliseconds this way to adjust the time is not perfect,
but pretty darn close for short time intervals. I've also
tossed in a function to add leading zeros to single digit
numbers in the date string. Full code below.
--
Rob

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title>Time play</title>
<meta http-equiv="Content-Type"
content="text/html; charset=ISO-8859-1">

<script type="text/javascript">
function dateToString(d){
var dateString = myFullYear(d.getYear())
+ '/' + addZ(d.getMonth()+1)
+ '/' + addZ(d.getDate())
+ ' ' + addZ(d.getHours())
+ ':' + addZ(d.getMinutes())
+ ':' + addZ(d.getSeconds())
return dateString;
}

function myFullYear(y){
return ( y < 1900 )? y + 1900 : y;
}

function plus90(x){
d = new Date(x.form.date1.value)
d.setDate(d.getDate()+90);
x.form.date2.value = dateToString(d);
document.getElementById('msgBox').innerHTML = dateToString(d);
}

function addTime(x){
var tBit = x.form.timeChange.value.split(':');
var changeInMilliseconds = 1000 * (
tBit[0] *60*60
+ tBit[1] *60
+ tBit[2] * 1);

d.setTime(d.getTime() + changeInMilliseconds);
x.form.date2.value = dateToString(d);
document.getElementById('msgBox').innerHTML = dateToString(d);
}

function addZ(z) {
return (z<10)? '0' + z : z;
}

</script>

</head>

<body>
<form action="">
<input type="text" name="date1" size="20"><br>
date (yyyy/mm/dd)<br><br>
<input type="text" name="date2" size="20" readonly><br>
90 days from date<br>
<input type="text" name="timeChange" size="20"
value="03:12:37">
<br>
<input type="button" value="Add 90 days"
onclick="plus90(this);">
<br>
<input type="button" value="Add time"
onclick="addTime(this);">
<br>
<input type="reset">
</form>
<span id="msgBox"></span>

<script type="text/javascript">
document.forms[0].date1.value = dateToString(new Date())
</script>

</body>
</html>
Jul 23 '05 #4
RobG wrote:
[...]
function myFullYear(y){
return (y<1900)?y+1900:y;
}


Ooops, the above works fine in Mozilla-based browsers for all
relevant (post 1 AD) years, however it will fail in IE for dates
below 1900. A better (but not perfect) myFullYear is:

function myFullYear(y){
return ( y < 999 )? y + 1900 : y;
}

This still works fine in the 'Zillas, but will give erroneous
results in IE for years before 999. Presumably any usage related
to eBay does not require coverage for this time frame. :-x

For further information, read here:

<URL:http://www.merlyn.demon.co.uk/js-date0.htm#gFY>

--
Rob
Jul 23 '05 #5
JRS: In article <11*********************@g14g2000cwa.googlegroups. com>,
dated Thu, 24 Feb 2005 13:10:14, seen in news:comp.lang.javascript,
Samir <dl*****@yahoo.com> posted :
Here's the Original Code, I am trying to change to use in a more useful
form for me, and ebay too.

<form>
<input type="text" name="date1" size="20"><br>
date (yyyy/mm/dd)<br><br>
<input type="text" name="date2" size="20" readonly><br>
90 days from date<br><br>
<input type="button" value="Calculate"
onclick="plus90(this);">
</form>

<script type="text/javascript">

document.forms(0).date1.value = dateToString(new Date())

function dateToString(d){
return [d.getYear(),d.getMonth()+1,d.getDate()].join('/')

}

function plus90(x){
d = new Date(x.form.date1.value)
d = new Date(d.getYear(),d.getMonth(),d.getDate()+90)
x.form.date2.value = dateToString(d)

}

</script>

You should validate the input date. In a Murrican-infested world, you
must expect to get dates written as 2/25/05, whatever you ask for.
Validation can return a Date Object. But perhaps you have not yet
bothered to read the newsgroup FAQ with care? See sig.

Function dateToString should give YYYY-MM-DD, since that is the
standard. So you should use a leading zero function. I doubt whether
it is worth creating an array and then joining it; I would use
with (d)
return getFullYear()+'-'+LZ(getMonth()+1)+'-'+LZ(getDate())

Input should match that, so use .replace(/-/g, "/") or similar; but,
in combining getting an Object with validation, that becomes irrelevant.

It is strange that you have learned that new Date() accepts
extended-range fields but are not using setDate() in that manner -
d.setDate(d.getDate()+90)

Better, IMHO, to pass this.form into plus90()

Better to use a named form rather than relying on it being forms(0)
- should that be forms[0] anyway?

--
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.htm> jscr maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.
Jul 23 '05 #6
JRS: In article <VI**************@news.optus.net.au>, dated Fri, 25 Feb
2005 03:12:53, seen in news:comp.lang.javascript, RobG
<rg***@iinet.net.auau> posted :

function plus90(x){
d = new Date(x.form.date1.value)
d.setDate(d.getDate()+90);
That gives the same local time (if it exists; 1 chance in 8766 currently
that it does not) 90 days on.

given the above, subtracting days should be simple easy. As for
time, you should convert the change in hours, minutes and
seconds to milliseconds, then use:

var changeInMilliseconds = 1000 * (
hoursEntered *60*60
+ minutesEntered *60
+ secondsEntered * 1);

d.setTime(d.getTime() + changeInMilliseconds);


That moves ahead by a certain absolute interval.

If the hours entered are 90*24 = 2160, then there is about a 50% chance,
in many locations, that it will not give the same result as plus90.

The OP should be aware of this.

--
John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 MIME.
Web <URL:http://www.merlyn.demon.co.uk/> - w. FAQish topics, links, acronyms
PAS EXE etc : <URL:http://www.merlyn.demon.co.uk/programs/> - see 00index.htm
Dates - miscdate.htm moredate.htm js-dates.htm pas-time.htm critdate.htm etc.
Jul 23 '05 #7
JRS: In article <o_**************@news.optus.net.au>, dated Fri, 25 Feb
2005 03:31:32, seen in news:comp.lang.javascript, RobG
<rg***@iinet.net.auau> posted :
A better (but not perfect) myFullYear is:

function myFullYear(y){
return ( y < 999 )? y + 1900 : y;
}

For current dates, return 2000 + y%100 works with all three
behaviours of getYear() that I've heard of.

--
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.htm> jscr maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.
Jul 23 '05 #8

Lu********@hotmail.com
*** Sent via Developersdex http://www.developersdex.com ***
Jul 23 '05 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Richard Hollenbeck | last post by:
I'm trying to write some code that will convert any of the most popular standard date formats twice in to something like "dd Mmm yyyy" (i.e. 08 Jan 1908) and compare the first with the second and...
28
by: Steve | last post by:
Hi all How would I find out the average date when given a bunch of dates? For example, I want to find the average length in time from the following dates:...
4
by: Russell | last post by:
I have an assignment that I have to complete. I have to write a windows app in C#. Here is the spec: 1/ Date Comparison Build a program that will find the number of days between two dates. You...
7
by: Adrian | last post by:
I hit on this problem converting a VB.NET insurance application to C#. Age next birthday calculated from date of birth is often needed in insurance premium calculations. Originally done using...
29
by: james | last post by:
I have a problem that at first glance seems not that hard to figure out. But, so far, the answer has escaped me. I have an old database file that has the date(s) stored in it as number of days. An...
5
by: Simon Dean | last post by:
Probably being a little thick here, but when you subtract one date away from another, how do you convert the resultant value into a number of days... I guess I could easily / 60 / 60 / 24... but...
17
by: Terry Jolly | last post by:
New to C# ---- How do I convert a Date to int? In VB6: Dim lDate as long lDate = CLng(Date) In C#
4
by: YardDancer | last post by:
Dear All, I am upgrading my skills from VB 6.0 to VB.Net. They say you can do all date and time calculations using the properties and methods of DateTime structure withour recourse to the...
0
yasirmturk
by: yasirmturk | last post by:
Standard Date and Time Functions The essential date and time functions that every SQL Server database should have to ensure that you can easily manipulate dates and times without the need for any...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.