date -> formatted string in a labkey validation script?

LabKey Support Forum (Inactive)
date -> formatted string in a labkey validation script? Ben Bimber  2010-10-15 12:41
Status: Closed
 
in a validation script I am creating a generating the description field (basically a multi-line summary of that record). I want to do something like 'Date: XXXX-XX-XX'. Note this is formatted.

the available date function is validation scripts is a little quirky, so below is what I came up with. it seems to work for single record imports (ie. 'insert new'). however through the ETL a lot of dates become 'NA-NA-NA'. Could this be related to what type of date object is being passed? in other situations we had to do the following to parse a date: new java.util.Date(java.util.Date.parse(row.Date));

I'm hoping to figure out the least fragile approach that will work no matter what import path the data uses. thanks for any help or suggestions.

here's what i have:


    dateString: function (date){
        //TODO: do better once more date functions added
        if(date){
            date = new Date(date);

            return (date.getFullYear() ? date.getFullYear()+'-'+(date.getMonth()<10 ? 0:'')+date.getMonth()+'-'+(date.getDate()<10 ? 0:'')+date.getDate() : '');
        }
        else
            return '';
    },
    dateTimeString: function (date){
        if(date){
            date = new Date(date);
            return date.getFullYear()+'-'+(date.getMonth()<10 ? 0:'')+date.getMonth()+'-'+(date.getDate()<10 ? 0:'')+date.getDate()
                    ' '+date.getHours()+':'+date.getMinutes();
        }
        else
            return '';
    },
 
 
jeckels responded:  2010-10-18 17:26
Hi Ben,

Yes, this looks like a good approach. I tested it in a few interesting cases and it worked correctly. Your code uses the JavaScript Date class, which is preferable to anything that reaches out and specifically uses Java classes.

It looks like there are some good standalone JavaScript date formatting libraries out there, like:

http://blog.stevenlevithan.com/archives/date-time-format

that might be worth including in your utility code. Of course, going forward it would be great to automatically have access to the relevant Ext libraries.

Thanks,
Josh
 
Ben Bimber responded:  2010-10-18 19:19
hi josh,

it seems like including those libraries is something that should get built into labkey itself. exactly what date library is used doesnt matter all that much, but some basic date manipulation would be a useful feature in validation scripts.