简体   繁体   English

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

[英]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”方法返回楠:

  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; 确保客户端没有完全中断的JavaScript实现;并且
  3. There is no obscure -- eg custom -- "Date" object be used on said funky client(s) 没有晦涩的地方-例如自定义-在上述时髦的客户端上使用“日期”对象

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.

相关问题 Safari Javascript Date() NaN 问题 (yyyy-MM-dd HH:mm:ss) - Safari Javascript Date() NaN Issue (yyyy-MM-dd HH:mm:ss) 要显示的日期格式<DD-MMM-YYYY HH:MM:SS>在 jqgrid 中显示为 NaN-undefined-NaN 12:NaN:NaN - Date format to be displayed in <DD-MMM-YYYY HH:MM:SS> in jqgrid displaying as NaN-undefined-NaN 12:NaN:NaN yyyy-mm-dd HH:mm:ss 格式的 Javascript 日期现在 (UTC) - Javascript Date Now (UTC) in yyyy-mm-dd HH:mm:ss format 如何在 javascript 中解析 yyyy-MM-dd HH:mm:ss.SSS 格式的日期? - How to parse yyyy-MM-dd HH:mm:ss.SSS format date in javascript? 如何使用JavaScript将数据格式“ YYYY-mm-dd hh:mm:ss”转换为“ dd-mm-YYYY hh:mm:ss”? - How to convert data format “YYYY-mm-dd hh:mm:ss” to “dd-mm-YYYY hh:mm:ss” using javascript? 格式日期从 "M/D/YYYY HH:mm:ss" 到 "YYYY/MM/dd HH:mm" vanilla javascript - format date from "M/D/YYYY HH:mm:ss" to "YYYY/MM/dd HH:mm" vanilla javascript Javascript'yyyy-mm-dd hh:mm:ss'到时间戳 - Javascript 'yyyy-mm-dd hh:mm:ss' to timestamp 如何在核心 JavaScript 中将新日期 object 格式化为 MySQL 格式 YYYY-MM-DD hh-mm-ss? - How to format new date object to MySQL format YYYY-MM-DD hh-mm-ss in core JavaScript? 如何使用javaScript将UTC日期时间转换为yyyy-mm-dd hh:mm:ss日期时间格式? - How to convert UTC date time into yyyy-mm-dd hh:mm:ss date time format using javaScript? 如何检查字符串是否为有效日期格式(格式应为:YYYY-MM-DD HH:mm:ss) - how to check the string is valid date format or not (format should be in : YYYY-MM-DD HH:mm:ss)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM