With a little side of applesauce...

Friday, August 6, 2010

Javascript - parse MS SQL timestamp for creating a Date object in Javascript

I wrote this Javascript function to aid in creating the equivalent Javascript Date object:
function getJsTimestamp( timeString ) {
// split the mssql timestamp, and return it so that we
// can create a date in javascript
var arrMssqldate = timeString.split( ' ' );
var arrDate = arrMssqldate[0].split( '-', 3 );
var arrTime = arrMssqldate[1].split( ':', 2);

var timeObject = new Object;
timeObject.year = arrDate[0];
timeObject.month = arrDate[1];
timeObject.day = arrDate[2];
timeObject.hour = arrTime[0];
timeObject.minute = arrTime[1];
timeObject.second = '00';

return timeObject;

}


It is quick and dirty, and defaults seconds to '00'. The example MS SQL Server timestamp string looks like '2010-06-01 07:00:00.0'.

Here is the example Javascript usage:
var timeObject = getJsTimestamp( "2010-06-01 07:00:00.0" );
d1[0][0] = new Date(timeObject.year, timeObject.month, timeObject.day, timeObject.hour, timeObject.minute, timeObject.second);

No comments: