The CreatorCon Call for Content is officially open! Get started here.

Chuck Tomasi
Tera Patron

find_real_file.pngI don't know about you, but one of the most troublesome issues I have with JavaScript is manipulating time related variables. It seems that time zones are either applied when I don't want them, or not applied when I want them. Recently a customer came up with a request to have a date variable on a record producer saved in a date_time field on a form. "But Chuck, why wouldn't you just put a date_time variable on the record producer and let it map to the record field?" Don't ask - long story. Suffice it to say - sometimes you just have to give the customer what they ask for. Once again I found myself thumbing through examples in the forums. After trying three different approaches, I found a solution.


FYI - the record producer was creating a Change Request (change_request) record. The change request has a date_time field "Requested End Date" (u_requested_end_date) and the record producer has a date variable called "Requested Date" (requested_date.)

The first thing I foolishly tried was:



current.u_requested_end_date = producer.requested_date;


It worked about as well as a screen door on a submarine!

Next I tried converting them to GlideDate and GlideDateTime, but ran in to the timezone offset. I would enter "2011-12-21" in the variable and get back "2011-12-20 18:00:00". Frustrating.

I even tried using the gs.dateGenerate() function, but it too had a timezone offset issue. See - I told you this was fun!

Finally I came up with a solution that set a GlideDateTime variable, then took the numeric value and placed that in the resulting GlideDateTime field.



var myDate = producer.requested_date;
var gdt = new GlideDateTime();
gdt.setDisplayValue(myDate);
current.u_requested_end_date.setDateNumericValue(gdt.getNumericValue());


Since we had other record producer to create and do the same conversion, I created an on demand function to house the guts of this.



function u_date_to_datetime(dateStr) {
if (dateStr == '')
return;
var gdt = new GlideDateTime();
gdt.setDisplayValue(dateStr);
return gdt.getNumericValue();
}


then use it like this:



current.u_requested_end_date.setDateNumericValue(u_date_to_datetime(producer.requested_date));


Now when I enter "2011-12-21" the resulting change record has "2011-12-21 00:00:00", just what I want!

I know, this is a bit of an odd example. The more I use this stuff, the more I start to understand the classes, methods available, and potential solutions. Have a good TIME!

14 Comments