javascript 格式日期 obj 为 yyyy-MM-dd hh:mm:ss 返回 NAN-NAN-NAN
我正在使用 javascript new Date() 并尝试将其格式化为字符串,如下所示:
yyyy-MM-dd hh:mm:ss
然后将其插入 SQLite 数据库中。我正在使用 Appcelerator (1.7.2),它使用 Mozilla Rhino 引擎来处理 JS。
这是函数:
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;
};
我无法自己重新创建此功能,该功能似乎始终对我有用,但某些客户的数据库显示 NAN-NAN-NAN NAN:NAN:NAN.NAN 并且这种情况可能会持续长达 40 分钟,然后再次开始工作。
以下是在 Navicat 中查看的 SQLite 数据库字段的屏幕截图:
date.getUTCHours() 或 date.getUTCMinutes() 可以返回 NAN:NAN 吗?
我必须使用 UTC 吗?我的客户在中东。
这是一个在 iOS / iPad2 设备上运行的 Titanium Appcelerator 应用程序。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
虽然代码有点......令人讨厌......任何工作 JavaScript 的 Date 对象都不可能从“getTimePart”方法返回 NaN:
也有可能所述环境只是破坏了“getUTCTimePart”方法;非 UTC 组件是否会重复出现这些症状?
快乐编码。
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.