[thelist] Use client-side date and time in asp.net

David T-G davidtg-thelist at justpickone.org
Sun May 9 20:05:59 CDT 2004

Jeff, et al --

...and then Jeff Ervin said...
% I have seen references to using the client's date and time in an asp.net
% page, but nothing complete.

Note that that really has nothing to do with ASP; the magic is in the
javascript that is sent down to the client.

% Below is the code taken from an earlier archive:
% <script>
% function SetDateTime() {
% 	document.frmAddEditNewRequests.HiddenDateTime.value = new Date();
% 	// Change myForm to the name of your Form
% }
% </script>

This part is the javascript.  It says that when this function, called
SetDateTime, is run, it will set the value of the field HiddenDateTime in
the frmAddEditNewRequests object (probably a form) to the current date --
as seen by the browser, that is.

% In the form tag add:
% 	onSubmit="SetDateTime()"

In order to make that function do something, you have to actually call
it.  This says that when the submit button is pushed the browser should
also execute that javascript function.

% In your form add:
% 	<input type="hidden" name="HiddenDateTime" value=""

This just defines the field in your form.  You could call it MyDate or
TheLocalTime or gOOfbAll if you wanted -- as long as you also changed the
name up above in the javascript.

% I don't understand how to reference HiddenDateTime.
% please help, if possible.

I speak PHP, so I'll give a PHP example.  Your page would look something

  <!DOCTYPE html PUBLIC '-//W3C//DTD HTML 4.0 Transitional//EN'
      <meta http-equiv='content-type' content='text/html; charset=iso-8859-1'>
      <title>What time is it?</title>
      <script language='javascript'>
        function setdatetime()
          { document.timeform.pctime.value = new Date(); }
    if ( isset($_POST['pctime']) )
      print "The time on your PC is " ;
      print "&gt;&gt;{$_POST['pctime']} &lt;&lt;.<br>\n" ;
      print "What time is it?<br>\n" ;
      print "<form method='post' name='timeform' onsubmit='setdatetime()'>\n" ;
      print "Input anything here; it does not matter.<br>\n" ;
      print "<input type='text' name='textinput' size='20'><br>\n" ;
      print "Say anything you want in the form.<br>\n" ;
      print "<input type='hidden' name='pctime' value=''>\n" ;
      print "Finally, ...\n" ;
      print "<input type='submit' value='Click to find out!'><br>\n" ;
      print "</form>\n" ;

which you can see in action at


and read in source at


as well.  Note that I called my field pctime instead of HiddenDateTime;
it's just a name.

% Thanks


David T-G
davidtg at justpickone.org
http://justpickone.org/davidtg/      Shpx gur Pbzzhavpngvbaf Qrprapl Npg!

More information about the thelist mailing list