Skip to content

Instantly share code, notes, and snippets.

@bennadel
Created October 2, 2012 14:28
Show Gist options
  • Save bennadel/3819554 to your computer and use it in GitHub Desktop.
Save bennadel/3819554 to your computer and use it in GitHub Desktop.
Using JavaScript to Submit UTC Dates To The ColdFusion Server
<!--- Param the form values. --->
<cfparam name="form.submitted" type="boolean" default="false" />
<cfparam name="form.utc" type="string" default="" />
<!--- ----------------------------------------------------- --->
<!--- ----------------------------------------------------- --->
<!--- Check to see if the form has been submitted. --->
<cfif (
form.submitted &&
isNumeric( form.utc )
)>
<!--- Create a local date from the UTC value. --->
<cfset localDate = createObject( "java", "java.util.Date" ).init(
javaCast( "long", form.utc )
) />
<!---
Convert the local date to a UTC date (my server / computer
currently runs in EST).
--->
<cfset utcDate = dateConvert( "local2utc", localDate ) />
<!--- Store the UTC entry. --->
<cfquery name="insertUtc">
INSERT INTO utc_testing
(
utc,
createdAt
) VALUES (
<cfqueryparam value="#utcDate#" cfsqltype="cf_sql_timestamp" />,
<cfqueryparam value="#now()#" cfsqltype="cf_sql_timestamp" />
);
</cfquery>
</cfif>
<!--- ----------------------------------------------------- --->
<!--- ----------------------------------------------------- --->
<!--- Query for all the dates. --->
<cfquery name="utcEntries">
SELECT
id,
utc,
createdAt
FROM
utc_testing
ORDER BY
createdAt DESC
</cfquery>
<!--- ----------------------------------------------------- --->
<!--- ----------------------------------------------------- --->
<!--- Reset the output buffer. --->
<cfcontent type="text/html; charset=utf-8" />
<cfoutput>
<!doctype html>
<html>
<head>
<title>Submitting UTC Times To The ColdFusion Server</title>
</head>
<body>
<h1>
Submitting UTC Times To The ColdFusion Server
</h1>
<!--- For refererence. --->
<p>
Now: #now()#<br />
UTC: #dateConvert( "local2utc", now() )#
</p>
<form method="post" action="#cgi.script_name#">
<!--- Flags the form submission. --->
<input type="hidden" name="submitted" value="true" />
<!---
The UTC milliseconds - what's *really* being
submitted back to the server as the date/time.
--->
<input type="hidden" name="utc" value="" />
<p>
Enter your local date/time:
<input type="text" name="date" size="17" />
at
<input type="text" name="time" size="22" />
</p>
<p>
<input type="submit" value="Submit Time" />
<!--- In case we mess up the form values. --->
<a href="#cgi.script_name#">Reset</a>
</p>
</form>
<!--- Dump out the current entries. --->
<cfdump
var="#utcEntries#"
label="UTC Entries"
/>
<script type="text/javascript" src="./jquery-1.8.2.min.js"></script>
<script type="text/javascript">
// Get and cache the DOM references we'll need.
var dom = {
form: $( "form" ),
utc: $( "input[ name = 'utc' ]"),
date: $( "input[ name = 'date' ]"),
time: $( "input[ name = 'time' ]")
};
// Set the default values for the date and time fields.
dom.date.val(
(new Date()).toDateString()
);
dom.time.val(
(new Date()).toTimeString()
);
// List for the form submission. When the user submits
// the form, we want to convert their local date/time
// value into UTC millisecodns so we can send back the
// universal time value.
dom.form.submit(
function( event ) {
// Try to parse a valid date from the user input.
// This will return the number of milliseconds
// from 1970, UTC.
var localUtc = Date.parse(
dom.date.val() + " " +
dom.time.val()
);
// Make sure the value is numeric. This means the
// date could be parsed.
if ( isNaN( localUtc ) ) {
// Oop, invliad date.
alert( "Could not parse your date." );
// Stop the form from submitting.
return( event.preventDefault() );
}
// If we made it this far, the date could be
// parsed - store it in the form (and then
// let the form submission happen).
dom.utc.val( localUtc );
}
);
</script>
</body>
</html>
</cfoutput>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment