Obtain Server Date and Time

From SmartWiki
Jump to: navigation, search

There are several methods to obtain the server date and time.

Variable Method

The current server date and time can be accessed using a variable:

@datetime(currentdate)@
  • See here for details

This returns the date and time formatted as yyyy-mm-dd HH:mm:ss and is often used to compare against deadlines. For example, a Read Only System Variable with the following content would show a bright yellow box if the current date is past the deadline of 5pm on January 1st 2014:

Note: this assumes you have created a System Variable called Submission Deadline with a value of 2014-01-01 17:00:00

<!--@sslogic("@datetime(currentdate)@">"@system.Submission Deadline@")-->
   <div style="border:2px #FF0000 solid;background-color:#FFFF00;padding:10px">
   The current date and time is @datetime(currentdate)@
   The Submission Deadline of @system.Submission Deadline@ has passed
   </div>
<!--@else-->
   The Submission Deadline is @system.Submission Deadline@
<!--@end-->


JavaScript Method

The variable method will pull the date and time from the server at the time the page loads. If you wish to obtain the server date and time at the time when the form is saved it can be done using JavaScript.

  • Note: When enforcing submission deadlines for security reasons it is recommended to use Submit Logic, although it can be supplemented with one of the methods described on this page.

The following JavaScript command will obtain the server date and time

var datetime=getservertime(false,"");

The following commands can be used to obtain various constituent portions of the server date and time:

  • datetime.getfieldbyname(1,"servertime") --> returns the server time in YYYY-MM-DD HH:MM:SS
  • datetime.getfieldbyname(1,"year") --> returns the year of the server time
  • datetime.getfieldbyname(1,"month") --> returns the month of the server time
  • datetime.getfieldbyname(1,"day") --> returns the day of the server time
  • datetime.getfieldbyname(1,"hour") --> returns the hour of the server time
  • datetime.getfieldbyname(1,"minute") --> returns the minute of the server time
  • datetime.getfieldbyname(1,"second") --> returns the second of the server time


For example, to check current server time against the submission deadline described above you could use the following JavaScript function:

function sbfunc(){

   var rs=getservertime(false,"");
   if(rs.getfieldbyname(1,"servertime")>="@system.Submission Deadline@"){
      alert("The submission deadline of @system.Submission Deadline@ has passed. Submission is not permitted");
      return false;
   }

   return true;
}


  • It is possible to get the date and time from the client machine using JavaScript's built-in Date() function, but using the server date and time is usually preferred since it is consistent and controlled.

See Also