Click to See Complete Forum and Search --> : Date object, including HOUR & MINUTES


cygnusx1z
06-11-2003, 02:01 PM
Hello everyone,

Recently I received great help on this forum on writing a script to "default populate" a text field in a form with the current date. It has worked perfectly. Of course now my users want more.

What I'd like the script to do now is to include the HOUR and MINUTES as well as the date. This is what I have now:

Date.prototype.toString = function () {
return [this.getMonth() < 10 ? '0' + (this.getMonth()+1) : (this.getMonth()+1), this.getDate() < 10 ? '0' +
this.getDate() : this.getDate(), this.getFullYear()].join('/')
}

Any help will be, as always, greatly appreciated!

Khalid Ali
06-11-2003, 02:04 PM
just add
this.getHours()+":"+this.getMinites();
to wards the end

Jona
06-11-2003, 02:07 PM
/*Untested code, it would help if you posted your form as well, please.*/

Date.prototype.toString = function () {
return [[this.getMonth() < 10 ? '0' + (this.getMonth()+1) : (this.getMonth()+1), this.getDate() < 10 ? '0' +
this.getDate() : this.getDate(), this.getFullYear()].join('/'), this.getHours(), this.getMinutes()].join(':')
}


Jona

Jona
06-11-2003, 02:08 PM
Khalid, wouldn't adding that put an extra slash at the end making it mm/dd/yyyy/hh:mm?

Jona

Charles
06-11-2003, 02:16 PM
Date.prototype.toString = function () {
return [[this.getMonth() < 10 ? '0' + (this.getMonth()+1) : (this.getMonth()+1), this.getDate() < 10 ? '0' +
this.getDate() : this.getDate(), this.getFullYear()].join('/'), [this.getHours(), this.getMinutes() < 10 ? '0' + this.getMinutes() : this.getMinutes()].join(':')].join(' ')}

Jona
06-11-2003, 02:18 PM
Well, I was close anyways... Charles, mind explaining where you learned to script? I understand how it works (you've explained it to me before), but I was just interested in where you learned JavaScript.

Jona

cygnusx1z
06-11-2003, 02:42 PM
Wow, you guys are quick!

Thanks for all your help.

This is the complete code with the new date script:
(I've deleted some information to protect the guilty)

<html>
<head>
<meta name="GENERATOR" content="Microsoft FrontPage 5.0">
<meta name="ProgId" content="FrontPage.Editor.Document">
<meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
<title>Center Electronic Tally Form</title>
<SCRIPT LANGUAGE="JavaScript">
<!--
// redefine the default Date Format
Date.prototype.toString = function () {
return [[this.getMonth() < 10 ? '0' + (this.getMonth()+1) : (this.getMonth()+1), this.getDate() < 10 ? '0' +
this.getDate() : this.getDate(), this.getFullYear()].join('/'), [this.getHours(), this.getMinutes() < 10 ? '0' + this.getMinutes()
: this.getMinutes()].join(':')].join(' ')}

//Validate that fields are completed
function validateFields() {
errmsg = "";

if (document.electTallyForm.csssname.selectedIndex == 0) {
errmsg += "Please select a NAME\n";
electTallyForm.csssname.focus()
}

if (document.electTallyForm.work_func.selectedIndex == 0) {
errmsg += "Please select a WORK FUNCTION\n";
electTallyForm.work_func.focus()
}

if (document.electTallyForm.ordrnum.value == "") {
errmsg += "Please enter an ORDER NUMBER\n";
electTallyForm.ordrnum.focus()
}

if (errmsg != "") {
alert (errmsg);
return false;
}

}

//-->
</script>

</head>

<body bgcolor="#CCCC00">
<SCRIPT LANGUAGE="JavaScript">
<!--
var never = new Date()
never.setTime(never.getTime() + 2000*24*60*60*1000);

// name is a string of the name of your cookie
// value is the value corresponding to name
function SetCookie(name, value) {
var expString = "; expires=" + never.toGMTString();
document.cookie = name + "=" + escape(value) + expString;
}

// returns value of cookie or null if cookie does not exist
function GetCookie(name) {
var result = null;
var myCookie = " " + document.cookie + ";";
var searchName = " " + name + "=";
var startOfCookie = myCookie.indexOf(searchName);
var endOfCookie;
if (startOfCookie != -1) {
startOfCookie += searchName.length; // skip past name of cookie
endOfCookie = myCookie.indexOf(";", startOfCookie);
result = unescape(myCookie.substring(startOfCookie, endOfCookie));
}
return result;
}

use_cookies = "unsure";

function saveValue(element) {
if (document.images && use_cookies == "unsure")
use_cookies = (confirm("Will you allow the values you enter into this form "
+"to be stored as cookies so that those values will be pre-filled the next "
+"time you return to this page?") ? "yes":"no");
if (document.images && use_cookies == "yes") {
if ((element.type == "text")
|| (element.type == "password")
|| (element.type == "textarea")
|| (element.type == "radio")) {
val = element.value;
} else if (element.type.indexOf("select") != -1) {
val = "";
for(k=0;k<element.length;k++)
if (element.options[k].selected)
val += k+" ";
} else if (element.type == "checkbox") {
val = element.checked;
}
SetCookie("memory_"+element.form.name+"_"+element.name,val);
}
}

function storedValues() {
if (document.images) { // only do it in JavaScript 1.1 browsers
for (i=0;i<document.forms.length;i++) {
for (j=0;j<document.forms[i].elements.length; j++) {
cookie_name = "memory_"+document.forms[i].name+"_"
+document.forms[i].elements[j].name;
val = GetCookie(cookie_name);
if (val) {
if ((document.forms[i].elements[j].type == "text")
|| (document.forms[i].elements[j].type == "password")
|| (document.forms[i].elements[j].type == "textarea")) {
document.forms[i].elements[j].value = val;
} else if (document.forms[i].elements[j].type.indexOf("select") != -1) {
document.forms[i].elements[j].selectedIndex = -1;
while (((pos = val.indexOf(" ")) != -1) && (val.length > 1)) {
sel = parseInt(val.substring(0,pos));
val = val.substring(pos+1,val.length);
if (sel < document.forms[i].elements[j].length)
document.forms[i].elements[j].options[sel].selected = true;
}
} else if (document.forms[i].elements[j].type == "checkbox") {
document.forms[i].elements[j].checked = val;
} else if (document.forms[i].elements[j].type == "radio") {
if (document.forms[i].elements[j].value == val)
document.forms[i].elements[j].checked = true;
}
}
}
}
}
}

onload = function(){
electTallyForm.date.value=new Date(); storedValues()
}

// -->
</script>

<!------------------------------------ MAIN AUTHORING AREA ------------------------------------->
<TD WIDTH="445" VALIGN="top" ALIGN="left">
<H2 ALIGN="left">Center Electronic Tally Form:</H2>
<p ALIGN="left"><font size="2">Please complete one form for each &quot;piece&quot; of work
completed. All fields denoted with an asterisk ( <font color="#FF0000">*</font>
) <font color="#FF0000">are required</font>. Be certain to include a project
name for any &quot;Special Projects&quot; completed.</font></p>
<FORM NAME="electTallyForm" METHOD="POST" action="--WEBBOT-SELF--" onsubmit="return validateFields()">
<!--webbot bot="SaveResults" s-label-fields="TRUE" b-reverse-chronology="FALSE" s-builtin-fields u-file="tally_results_mesa.txt" s-format="TEXT/CSV" startspan --><input TYPE="hidden" NAME="VTI-GROUP" VALUE="0"><!--webbot bot="SaveResults" endspan i-checksum="43374" -->

<DIV ALIGN="center">
<CENTER>
<TABLE BORDER="1" CELLPADDING="3" CELLSPACING="1" width="436">
<!---------------------------------Name & Work Function Row--------------------------------------->
<TR ALIGN="right">
<TH BGCOLOR="#FFCC66" colspan="2" width="425">
<p align="center">Center Tally Form</TH>
</TR>

<TR ALIGN="right">
<TH BGCOLOR="#FFCC66" width="179"><font color="#FF0000">*</font>Name:</TH>
<TD BGCOLOR="#FFCC66" width="237">
<p align="left">
<SELECT NAME="csssname" tabindex="1" onChange="saveValue(this)">
<OPTION>--Select One--</OPTION>
</SELECT>
</TD>
</TR>

<!--------------------------------DATE & ORDER NUMBER Row--------------------------------------------->
<TR ALIGN="right">
<TH BGCOLOR="#FFCC66" width="179"><P ALIGN="right"><font color="#FF0000">*</font>Date:</TH>
<TD BGCOLOR="#FFCC66" ALIGN="left" width="237">
<INPUT NAME="date" SIZE="18" onchange="this.value = new Date(this.value)" tabindex="2">&nbsp;<TT>MM/DD/YYYY</TT>
<INPUT NAME="time" TYPE="hidden" onchange="this.value = new Date(this.value)">
</TD>
</TR>

<!-------------------------------Work Function Row---------------------------------------------------------->
<TR>
<TH BGCOLOR="#FFCC66" width="179">
<p align="right"><font color="#FF0000">*</font>Work Function:</TH>
<TD BGCOLOR="#FFCC66" width="237">
<SELECT NAME="work_func" tabindex="3">
<OPTION>--Select One--</OPTION>
</SELECT>
</TD>
</TR>


<tr>
<TH BGCOLOR="#FFCC66" width="179">
<p align="right">Special Project Name:</TH>
<TH BGCOLOR="#FFCC66" width="237">
<p align="left">
<input type="text" name="specproj" size="20" value="If Applicable"></TH>
</tr>


<TR>
<TH BGCOLOR="#FFCC66" width="179"><P ALIGN="right"><font color="#FF0000">*</font>Order/Line Number:</TH>
<TD BGCOLOR="#FFCC66" width="237">
<INPUT TYPE="text" NAME="ordrnum" SIZE="20" tabindex="4">
</TD>
</TR>

<!----------------------------------Submit/Reset Row-------------------------------------------->
<TR>
<TD COLSPAN="2" ALIGN="center" BGCOLOR="#FFCC66" width="425">
<INPUT TYPE="SUBMIT" VALUE="SEND">
<INPUT TYPE="RESET" VALUE="CLEAR">
</TD>
</TR>

</TABLE>
</CENTER>
</DIV>
</FORM>

<p ALIGN="left"><i><font size="2">If you encounter any trouble while attempting
to complete this form you may email a detailed description of the trouble to the <a href="mailto:somerone@somecompany.com">Web
Coordinator</a>.</font></i></table>
</body>

</html>

Charles
06-11-2003, 02:56 PM
Be warned, the "language" attribute of the SCRIPT element was depricated in 1997 for a reason. If you use it then Netscape will treat Arrays in some non-standard ways. Use type="text/javascript" instead.From the Netscape Client-Side JavaScript Reference for version 1.3
In JavaScript 1.2 and earlier versions, toString returns a string representing the source code of the array. This value is the same as the value returned by the toSource method in JavaScript 1.3 and later versions.
http://devedge.netscape.com/library/manuals/2000/javascript/1.3/reference/array.html#1193921

cygnusx1z
06-11-2003, 03:04 PM
Charles,

Thanks again. I will take due note and use type= "text/javascript" going forward.