[英]bookshelf/knex add time to timestamp
使用NodeJS,Knex,Bookshelf JS ORM和PostgreSQL,我有一個字段表table.timestamp('reset_password_expires');
-如何設定未來的日期(假設現在是10分鍾)?
我試過了:
new User.User({'email': email}).fetch().then((user) => {
user.set('reset_password_expires', Date.now() + 60000);
user.save();
...
但這引發了:
未處理的拒絕錯誤:日期/時間字段值超出范圍:“ 1544577763253”
也嘗試過
user.set('reset_password_expires', 'NOW()' + '10 minutes'::interval);
但這是無效的語法。
我也嘗試將字段類型更改為dateTime
,結果相同。
我認為PostgreSQL的要求時間戳是在普通的日期時間格式(如描述在這里 )。 目前,您距紀元已過去了毫秒。
在開發人員工具中:
> Date.now() + 60000
1544706877041
您可以嘗試使用以下方法:
new Date((new Date()).getTime() + 60000)
在開發人員工具中:
> new Date((new Date()).getTime() + 60000)
Thu Dec 13 2018 14:15:50 GMT+0100 (Central European Standard Time)
這是我自己使用的實用程序功能:
module.exports.inHours = (hours) => {
const minutes = hours * 60;
const seconds = minutes * 60;
const milliseconds = seconds * 1000;
return new Date((new Date()).getTime() + milliseconds);
};
我的代碼看起來與您的代碼略有不同,但這就是我調用該函數的方式:
await userQueries.update(user.user_id, {
resetPasswordToken: resetToken,
resetPasswordExpires: inHours(1),
modified_at: new Date(),
});
這是日期時間,但是不是時間戳。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.