[英]javascript format Date obj to yyyy-MM-dd hh:mm:ss returns NAN-NAN-NAN
I am taking a javascript new Date() and trying to format it as a string like so: 我正在使用javascript新的Date()并尝试将其格式化为字符串,如下所示:
yyyy-MM-dd hh:mm:ss yyyy-MM-dd hh:mm:ss
and then insert it into a SQLite database. 然后将其插入SQLite数据库。 I am using Appcelerator (1.7.2) which uses the Mozilla Rhino engine for JS.
我正在使用将Mozilla Rhino引擎用于JS的Appcelerator(1.7.2)。
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. 我自己无法重新创建此功能,该功能似乎对我一直有效,但是某些客户的数据库显示NAN-NAN-NAN NAN:NAN:NAN.NAN,此过程可能会持续40分钟,然后再次开始工作。
Here is a screenshot of the field from a SQLite database as viewed in Navicat: 这是在Navicat中查看的来自SQLite数据库的字段的屏幕截图:
Can date.getUTCHours() or date.getUTCMinutes() return NAN:NAN? date.getUTCHours()或date.getUTCMinutes()是否可以返回NAN:NAN?
Do I have to use UTC? 我必须使用UTC吗? My clients are in the Middle East.
我的客户在中东。
This is an Titanium Appcelerator application running on iOS / iPad2 devices. 这是在iOS / iPad2设备上运行的Titanium Appcelerator应用程序。
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: 虽然代码是有点......恶心的......这是不可能的任何工作的 JavaScript的Date对象从“getTimePart”方法返回楠:
It may also be possible that said environment(s) just have broken "getUTCTimePart" methods; 所说的环境可能已经损坏了“ getUTCTimePart”方法; can the symptoms be repeated with the non-UTC components?
非UTC组件可以重复出现症状吗?
Happy coding. 快乐的编码。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.