I am taking a javascript new Date() and trying to format it as a string like so:
yyyy-MM-dd hh:mm:ss
and then insert it into a SQLite database. I am using Appcelerator (1.7.2) which uses the Mozilla Rhino engine for JS.
Here is the function:
date.DateToSQLite = function(date){
if (date == null){
return null;
} else if (date instanceof Date == false){
throw "not a date Object value:" + date;
};
var result = date.getUTCFullYear();
result += '-';
if (date.getUTCMonth() < 9){
result += '0';
}
result += date.getUTCMonth() +1;
result += '-';
if (date.getUTCDate() < 10){
result += '0';
}
result += date.getUTCDate();
result += ' ';
if (date.getUTCHours() < 10){
result += '0';
}
result += date.getUTCHours();
result += ':';
if (date.getUTCMinutes() < 10){
result += '0';
}
result += date.getUTCMinutes();
result += ':';
if (date.getUTCSeconds() < 10){
result += '0';
}
result += date.getUTCSeconds();
result += '.';
if (date.getUTCMilliseconds() < 100){
result += '0';
}
if (date.getUTCMilliseconds() < 10){
result += '0';
}
result += date.getUTCMilliseconds();
//Ti.API.info('date.DateToSQLite result:' + result + ' date:' + date);
return result;
};
I can’t recreate this myself, the function appears to always work for me, but some client’s databases show NAN-NAN-NAN NAN:NAN:NAN.NAN and this can go on for up to 40 minutes and then it starts working again.
Here is a screenshot of the field from a SQLite database as viewed in Navicat:


Can date.getUTCHours() or date.getUTCMinutes() return NAN:NAN?
Do I have to use UTC? My clients are in the Middle East.
This is an Titanium Appcelerator application running on iOS / iPad2 devices.
While the code is sort of … icky … it is not possible for any working JavaScript’s Date object to return NaN from the “getTimePart” methods:
It may also be possible that said environment(s) just have broken “getUTCTimePart” methods; can the symptoms be repeated with the non-UTC components?
Happy coding.