0

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;
devRena
  • 327
  • 2
  • 5
  • 12

3 Answers3

3

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.

Tim Biegeleisen
  • 502,043
  • 27
  • 286
  • 360
  • And/or just add the milliseconds instead of seconds instead of dividing by 1000? There is an ‘ms’ option for ‘dateadd’ if I remember correctly. – thomas Apr 19 '18 at 11:50
  • @thomas I wasn't sure of that, and you know how SO is, post quickly or lose. – Tim Biegeleisen Apr 19 '18 at 11:53
  • I hear ya! Just checked and there is a millisecond, microsecond, and nanosecond option. – thomas Apr 19 '18 at 11:55
1

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', ' ');
Zoe
  • 27,060
  • 21
  • 118
  • 148
AmitD
  • 11
  • 6
1

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;