Scholar.Date.sqlToDate(string sqldate) -- function to convert SQL-formatted date (e.g. '2006-06-13 11:03:05' or '2006-06-13') into a JS date object

This commit is contained in:
Dan Stillman 2006-06-13 15:07:08 +00:00
parent 39f9d2c3b3
commit ca2045a305

View file

@ -385,3 +385,33 @@ Scholar.HTTP = new function(){
}
}
}
Scholar.Date = new function(){
this.sqlToDate = sqlToDate;
/**
* Convert an SQL date in the form '2006-06-13 11:03:05' into a JS Date object
*
* Can also accept just the date part (e.g. '2006-06-13')
**/
function sqlToDate(sqldate){
try {
var datetime = sqldate.split(' ');
var dateparts = datetime[0].split('-');
if (datetime[1]){
var timeparts = datetime[1].split(':');
}
else {
timeparts = [false, false, false];
}
return new Date(dateparts[0], dateparts[1], dateparts[2],
timeparts[0], timeparts[1], timeparts[2]);
}
catch (e){
Scholar.debug(sqldate + ' is not a valid SQL date', 2)
return false;
}
}
}