【问题标题】:Time values from timestamp [sqlite3, Knex]来自时间戳 [sqlite3, Knex] 的时间值
【发布时间】:2021-06-02 16:27:13
【问题描述】:

如何在 created_at 行中仅存储 IST 时间值(如 HH:MM),从 timestamp 生成的值采用 UTC 和 DATE+TIME 格式

exports.up = function(knex) {
    return knex.schema.createTable('tbl_xyz', (table) => {
        table.increments('id').primary();
        table.timestamp('created_at').defaultTo(knex.fn.now());
        table.string('date');
        table.string('url');
        table.string('tags');
    });
};

【问题讨论】:

  • 您想在 IST 时区中只保存 HH:MM 还是同时保存日期和时间?
  • @fortunee 仅 HH:MM 时间
  • 你在使用 MySQL 还是 Postgres 什么数据库?
  • 哎呀我现在看到了,你正在使用 sqlite
  • 您可能想考虑使用moment.format() 功能

标签: mysql node.js sqlite knex.js


【解决方案1】:

试试date-fns:

const dateFns = require("date-fns")

然后,在表 def 上:

table.timestamp('created_at').defaultTo(dateFns.format(knex.fn.now(), 'hh:mm'))

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-01-07
    • 1970-01-01
    • 2019-07-14
    • 1970-01-01
    • 2017-06-15
    • 2017-03-19
    • 2014-05-22
    • 1970-01-01
    相关资源
    最近更新 更多