devRena
devRena

Reputation: 327

How to convert seconds to datetime format?

I have this variable var resetPasswordExpires = 1524133033085; and I want to convert seconds to datetime format to update database sql column. I did this

 var d = new Date();
 d.toString(resetPasswordExpires);

and i take d = Thu Apr 19 2018 12:21:02 GMT+0300 (GTB Daylight Time) and of course canot save column to database. Database column format is DATETIME

query :

UPDATE User SET resetPasswordToken = 
c2d313284f6de68cc1f18e3c45b0ea2c720220f0, resetPasswordExpires = Thu Apr 19 
2018 12:21:02 GMT+0300 (GTB Daylight Time) WHERE UserID = 21;

Upvotes: 0

Views: 5289

Answers (3)

AmitD
AmitD

Reputation: 11

I guess this is already answered in Convert JS date time to MySQL datetime. Please validate your requirement.

var resetPasswordExpires = 1524133033085;
var d = new Date(1524133033085).toISOString().slice(0, 19).replace('T', ' ');

Upvotes: 1

Hardik Masalawala
Hardik Masalawala

Reputation: 1066

TESTED and SOLVED

Convert your timestamp as following

try in Console

var resetPasswordExpires = 1524133033085;
var d = new Date();
d.toString(resetPasswordExpires);
var passDateToISOString  = d.toISOString();
passDateToISOString

Result in console

"2018-04-19T09:55:14.824Z"

then Pass passDateToISOString variable value to your sql

for Example ( try in Sql )

UPDATE User 
SET resetPasswordToken = c2d313284f6de68cc1f18e3c45b0ea2c720220f0, 
resetPasswordExpires = '2018-04-19T09:55:14.824Z' 
WHERE UserID = 21;

Upvotes: 1

Tim Biegeleisen
Tim Biegeleisen

Reputation: 521073

One option would be to DATEADD the number of seconds in your value to the start of the UNIX epoch:

SELECT DATEADD(s, 1524133033085/1000, '19700101 00:00:00:000');

19.04.2018 10:17:13

Demo

Note that I divide your value by 1000 because it is in milliseconds.

Upvotes: 3

Related Questions