javascript 格式日期 obj 为 yyyy-MM-dd hh:mm:ss 返回 NAN-NAN-NAN

发布于 2024-12-10 07:53:19 字数 1628 浏览 2 评论 0原文

我正在使用 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:

Showing errors in Database

Showing design view of database

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

宁愿没拥抱 2024-12-17 07:53:19

虽然代码有点......令人讨厌......任何工作 JavaScript 的 Date 对象都不可能从“getTimePart”方法返回 NaN:

  1. 确保客户端正在使用预期的代码(不是旧版本),以及;
  2. 确保客户端没有完全损坏的 JavaScript 实现,并且;
  3. 在所述时髦客户端上不存在晦涩的——例如自定义——“日期”对象。

也有可能所述环境只是破坏了“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:

  1. Ensure the client(s) are using the expected code (not an old version), and;
  2. Ensure the client(s) don't have an absolutely broken JavaScript implementation, and;
  3. There is no obscure -- e.g. custom -- "Date" object be used on said funky client(s)

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文