将 SQL 时间值传递给 Javascript?

发布于 2024-12-25 03:40:13 字数 244 浏览 4 评论 0原文

我正在使用融合图表。我正在尝试将 SQL 数组的元素附加到 Javascript strURL 上,以便它们可以传递到另一个页面。 siteid 和 name 等字段可以很好地传递,但每当我尝试传递时间戳时,它都无法加载图表。我无法完整解释整个事情,因为有太多的部分要写,但如果有人对为什么时间戳不能附加到 strURL 有任何想法,那么我很想知道。

返回值如下所示:2011-12-19 12:00:00

I'm using fusion charts. I'm trying to attach elements of an SQL array onto a Javascript strURL so they can be passed to another page. Fields such as siteid and name get passed fine but whenever I try to pass over the timestamp it fails to load the graph. I can't explain the whole thing in full as there are too many parts to write about but if anyone has any ideas as to why a timestamp cannot be attached to a strURL then I would love to know.

The returned value looks like: 2011-12-19 12:00:00

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

年少掌心 2025-01-01 03:40:13

该问题可能是由时间戳的形式引起的。如果它包含未正确编码的特殊字符,那么您就会遇到问题。

从数据库获取 Unix 纪元

您可以通过直接从数据库获取 Unix 纪元时间戳来避免这种情况(例如 MySQL):

SELECT UNIX_TIMESTAMP(`my_timestamp`) FROM `my_table`;

并且因为这样的时间戳应该只包含数字,所以您不应该遇到任何关于错误字符的问题。

在 JavaScript 中正确处理 Unix 纪元

在 JavaScript 中处理 Unix 纪元时间戳相当简单:

var db_timestamp = '1330655412';
var my_time = new Date();
my_time.setTime(db_timestamp * 1000);

请参阅 this jsfiddle 以获取证明。

The problem probably is caused by the form of the timestamp. If it contains special characters that are not encoded properly, then you have a problem.

Get Unix epoch from database

You can avoid this by getting Unix epoch timestamp directly from database like that (example for MySQL):

SELECT UNIX_TIMESTAMP(`my_timestamp`) FROM `my_table`;

and because such timestamp should only consist of digits, you should not have any problems regarding incorrect characters.

Process Unix epoch in JavaScript correctly

Processing Unix epoch timestamps in JavaScript is rather simple:

var db_timestamp = '1330655412';
var my_time = new Date();
my_time.setTime(db_timestamp * 1000);

See this jsfiddle for a proof.

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