如何解决检索执行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
});
这会自动将createdAt
和updatedAt
添加到新创建的Fruits
文档中。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。