jquery - 为什么从 Psql 通过 Swift (Vapor) 到通过 Ajax 的 JQuery 的日期落后了 31 年?
问题描述
在 Swift (Vapor) 服务器中,我从 Postgres 数据库中检索具有日期字段的对象。数据的正确性在数据库中得到验证。然后我对对象进行编码:
func sendToSocket(_ game: Game) {
if Network.ownerWS == nil { return }
var payload : String = ""
do {
payload = try String( data: JSONEncoder().encode(game), encoding: .utf8)!
} catch {
print("oops couldn't encode payload")
}
Network.ownerWS!.send(payload)
}
这会通过 Web 套接字将生成的对象发送到监控屏幕。
在该客户端上,消息由 JS/JQuery 函数接收,这很好:
ws.onmessage = function (evt) {
var received_msg = evt.data;
var game = JSON.parse(received_msg)
console.log(received_msg)
…
console.log(game.started)
console.log(new Date(game.started * 1000))
let format = [{day: 'numeric'}, {month: 'short'}];
var started = new Date(game.started, format);
var ended = new Date(game.ended);
console.log("started: " + started + " ended: " + ended)
…
};
第一个日志调用产生预期值:
custom.js:16{"player":{"id":"6CCE3047-8080-4793-83C0- C634C9E73AE1"},"score":34,"id":"4EA10020-79C6-46D0-AAD7-EC3E5C271E0D","moves":25,"match":{"id":"93D1319D-A258-4F9D-AC6E-B4860F95D948"},"updatedAt":649664452.29815102,"createdAt":649589977.37518704,"ended":null,"started":649661807.94521403}
这似乎是我的问题似乎开始的地方。开始的数值似乎不熟悉:它是从原始 Postgres 值以某种方式转换的,我不知道这是什么格式。
第二个日志条目显示来自 JSON 的值,第三个日志条目显示的日期正好比正确的原始日期早 31 年:
Fri Aug 03 1990 12:36:47 GMT+0700 (+07)
31 年的确切精度似乎是一些我不理解的格式约定。有没有人知道这里发生了什么脱轨?
这段代码的基本目标——更新页面上的值——在不涉及日期的情况下可以正常工作。
编辑:仅供参考,来自 postgres 的模式:
Column | Type | Collation | Nullable |
Default
------------+--------------------------+-----------+----------+---------
id | uuid | | not null |
match | uuid | | not null |
player | uuid | | not null |
started | timestamp with time zone | | |
ended | timestamp with time zone | | |
score | bigint | | |
moves | bigint | | |
created_at |timestamp with time zone | | |
updated_at |timestamp with time zone | | |
解决方案
我在这里积极关注任何答案,但今天的截止日期让我摆脱了这个问题:我会绕着它转,但也会尝试回答任何问题或想法......
推荐阅读
- javascript - 无法在小型 JavaScript 游戏中正确显示棋子 - JavaScript Canvas
- angular - NGXS - 在 switchMap 中返回(任何)时提供了无效对象
- bash - bash在用ifs分割线后打印整行
- python - 高效地将项目批量保存到 Elasticsearch
- wordpress - WooCommerce - 部分产品免费送货 - 使用 UPS Shipping 插件
- ios - 约束在 Present ViewController 中无法正常工作
- sql - 查询以对多对多联结表中的完全递归关系进行分组
- sql-server - 如何在 SQL Server 中使用 DataTable 模拟“批量”插入
- excel - 在值表中查找最接近 0 的正负值
- javascript - 从外部 .js 文件中读取以告诉 javascript 要显示哪个图像