Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Date calculation help

Status
Not open for further replies.

gunnie

IS-IT--Management
Apr 4, 2002
9
0
0
JP
I have the below form with javascript to calculate a hidden password based on the current year and day of the year. ie. 2006026 minus a 6 digit number = the hidden password

All I want to do is enter the 6 digit number and then calculate. Currently I have to enter the current date and the 6 digit number.. Is there a way to store the current date in the required format mm/dd/yyyy to a variable?

<html>
<head>
<script>
function Calculate()
{
today=new Date()
var EnteredMonth = MyForm.Date1.value.substr(0,2) - 1;
var EnteredDay = MyForm.Date1.value.substr(3,2);
var EnteredYear = MyForm.Date1.value.substr(6,4);
var EnteredDate = new Date(EnteredYear, EnteredMonth, EnteredDay, 0, 0, 0);
var EnteredSN = MyForm.SN.value;
today=new Date()
var startofyear=new Date(EnteredDate.getFullYear(), 0, 1)
var one_day=1000*60*60*24
var DayOfYear = Math.ceil((EnteredDate.getTime() - startofyear.getTime()) / one_day) + 1
var Calc = ((EnteredYear + "0") + DayOfYear)
var HiddPass = Calc - EnteredSN
MyForm.Pass.value = HiddPass;

}
</script>
<title>Untitled Document</title>
</head>

<body>
<font size="2" face="Arial">(Dates must be entered as mm/dd/yyyy i.e. 01/02/2003)<br>
</font>
<form name="MyForm">
<font face="Arial"><font size="2">Todays Date </font>

<input type="text" name="Date1" Value="" size="10">
<font size="2"> Last 6 digits
of number&nbsp;&nbsp; </font><Input type="text" name="SN" Size="6"><font size="2">
<br>
<br>
&nbsp;</font><input type="button" name="calc" value="Calculate" onClick="Calculate()"><font size="2">
</font>
<input type="reset" name="Reset" value="Clear"><font size="2">
Hidden Password is </font><Input type="text" name="Pass" Size="7"><font size="2">&nbsp;&nbsp;
<br>

<br>
</font></font>
<br>
</form>

</body>
</html>
 
I've not tested this, but it should work. Most of your code is still as-is - I've just tidied up things like:

- no use of semicolons
- inconsistent variable naming (vis-a-vis case)
- bad accessing of form elements

Anyway - replace your Calculate function with these two, and you'll negate the need to type in the date:

Code:
function padNum(num) {
	var s = num.toString();
	if (s.length < 2) return('0' + s);
	return(s);
}

function Calculate() {
	var today = new Date();
	var enteredMonth = padNum(today.getMonth() + 1);
	var enteredDay = padNum(today.getDate());
	var enteredYear = today.getFullYear();
	var enteredSN = document.forms['MyForm'].elements['SN'].value;
	var startOfYear = new Date(enteredYear, 0, 1);
	var oneDay = 1000 * 60 * 60 * 24;
	var dayOfYear = Math.ceil((today.getTime() - startOfYear.getTime()) / oneDay) + 1;
	var calc = ((enteredYear + '0') + dayOfYear);
	var hiddPass = calc - EnteredSN;
	document.forms['MyForm'].elements['Pass'].value = hiddPass;
}

Hope this helps,
Dan

[tt]Dan's Page [blue]@[/blue] Code Couch
[/tt]
 
Thanks heaps!

Works great. As you can see I am no programmer. Put what I had together from snippets of code I searched for.

Thanks again,
Craig
 
One thing to be aware of - relying on the client-side computer to provide an accurate date is ALWAYS bad - it can easily be changed, or be wrong.

You should utimately deliver it to the page from a web server if you need it to be less tamper-proof.

Dan



[tt]Dan's Page [blue]@[/blue] Code Couch
[/tt]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top