Click to See Complete Forum and Search --> : number to date
I'm looking for something that will allow me to enter a number, representing a day. I would also enter the year. The script would return the date representing the number and year entered.
Example : enter the number 272 and the the year 1998. The script should return the date September 29.
the 272nd day of 1998 was September 29.
Thanks,
Charles
11-17-2003, 04:48 PM
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<meta name="Content-Script-Type" content="text/javascript">
<title>Example</title>
<style type="text/css">
<!--
input {width:3em}
-->
</style>
<script type="text/javascript">
<!--
Date.ONE_SECOND = 1000;
Date.ONE_MINUTE = Date.ONE_SECOND * 60;
Date.ONE_HOUR = Date.ONE_MINUTE * 60;
Date.ONE_DAY = Date.ONE_HOUR * 24;
function updateDate () {
var d = new Date (document.getElementById('year').value, 0, 0);
d = new Date (d.getTime() + document.getElementById('days').value * Date.ONE_DAY);
document.getElementById('date').firstChild.data = d > 0 ? d.toDateString() : 'Out of range.';
}
function th (s) {
if (/1\d/.test(s)) return 'th';
if (/1$/.test(s)) return 'st';
if (/2$/.test(s)) return 'nd';
if (/3$/.test(s)) return 'rd';
return 'th';
}
// -->
</script>
<p>The <input type="text" id="days" onchange="this.nextSibling.firstChild.data = th(this.value); updateDate()"><span>&nbsp;</span> day of the <input type="text" id="year" onchange="this.nextSibling.firstChild.data = th(this.value); updateDate()"><span>&nbsp;</span> year of the common era.</p>
<p id="date">&nbsp;</p>
Charles
11-17-2003, 07:42 PM
Here's a version that I like better and is certainly easier to use.
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<meta name="Content-Script-Type" content="text/javascript">
<title>Example</title>
<style type="text/css">
<!--
input {width:3em}
-->
</style>
<script type="text/javascript">
<!--
Date.ONE_SECOND = 1000;
Date.ONE_MINUTE = Date.ONE_SECOND * 60;
Date.ONE_HOUR = Date.ONE_MINUTE * 60;
Date.ONE_DAY = Date.ONE_HOUR * 24;
function updateDate () {
var d = new Date (new Ordinal(document.getElementById('year').value), 0, 0);
d = new Date (d.getTime() + new Ordinal(document.getElementById('days').value) * Date.ONE_DAY);
document.getElementById('date').firstChild.data = d > 0 ? d.toDateString() : 'Out of range.';
}
function Ordinal (n) {this.n = Number (isNaN (n) ? n.toString().match(/\d+/) : n)}
Ordinal.prototype.valueOf = function () {return this.n}
Ordinal.prototype.toString = function () {
if (isNaN (this.n)) return NaN.toString();
s = this.n.toString();
if (/1\d/.test(s)) return s + 'th';
if (/1$/.test(s)) return s + 'st';
if (/2$/.test(s)) return s + 'nd';
if (/3$/.test(s)) return s + 'rd';
return s + 'th';
}
// -->
</script>
<p>The <input type="text" id="days" onchange="this.value = new Ordinal (this.value); updateDate()"> day of the <input type="text" id="year" onchange="this.value = new Ordinal (this.value); updateDate()"> year of the common era.</p>
<p id="date">&nbsp;</p>
Thanks, That works great!!