检索执行http发布请求的日期时间

问题描述

我有一个表和一个表单,用于检索数据并将其发送到数据库。提交表单后,将使用刚提交的数据创建一个新的表行。

我想为每行添加一个字段,其中包含提交的日期和时间。

如果我单击“提交”按钮,则会发出http发帖请求。

是否可以保存提交的时间和日期? 我在数据库中使用猫鼬,在路由器中使用快递。

  function framework(selector) {
        if (window === this) { return new framework(selector); }
    
        if (selector === 'document') { this.element = [document]; }
        else if (selector === 'window') { this.element = [window]; }
        else if (selector) { this.element = document.querySelectorAll(selector); }
    
        return this;
    };
    framework.prototype.append = function(node){
        if (this.element[0]){ this.element[0].appendChild(node); }
        return this;
    };
    framework.prototype.html = function(node){
        if (this.element[0]){ this.element[0].innerHTML(node); }
        return this;
    };
    
    
    var div = document.createElement('div');
    div.innerHTML = 'test append';
    framework("#example1").append(div);
    framework("#example2").html('test html');

解决方法

您可以使用Date对象获取日期和时间。

将以下代码传递到要保存的代码中。 (如MongoDB)

let date_ob = new Date();

详细了解Date

,

您可以使用猫鼬模型在服务器端设置POST请求的时间戳。

在您的mongoose Fruits Schema中,您可以添加一个timestamps字段,默认设置为new Date()。像这样:

const mongoose = require("mongoose");

const FruitSchema = new mongoose.Schema({
    ... other Fruit properties...
    timestamps: { type: Date,default: () => new Date() }
});

或者,您可以通过将第二个对象传递到mongoose中来利用timestamps内置的Schema,就像这样:

const mongoose = require("mongoose");

const FruitSchema = new mongoose.Schema({
    ... Fruit properties ...
},{
    timestamps: true
});

这会自动将createdAtupdatedAt添加到新创建的Fruits文档中。