'how to strip timestamp of +00:00 in sequelize
I am using Sequelize to map objects to my already existing database, in this database they didnt use the auto-generated timestamps, they did their own datetime work, the current way the database stores the date is in YYYY-MM-DD HH:MM:SS.MS
however using sequelize, i am returning a date that looks like YYYY-MM-DD HH:MM:SS.MS +00:00
and i believe this is whats tripping me up. as i am receiving an error Conversion failed when converting date and/or time from character string.
I tried even accessing the Date()
object and building my own date string, and i got the same error. even though it follows the correct formatting.
for my model i have this at the CreateDateTime
(This is the name of the field in the database.
CreateDateTime: {
type: DataTypes.DATE,
defaultValue: `${date.getFullYear()}-${date.getMonth()}-${date.getDay()} ${date.getHours()}:${date.getMinutes()}:${date.getSeconds()}.${date.getMilliseconds()}`,
},
UpdateDateTime: {
type: DataTypes.DATE,
},
I have also, renamed the timestamps that sequelize provides to the appropriate names above. I am not sure much more of how to get around this issue besides maybe using the timestamps provided by sequelize and dropping the ones in the existing.. though id rather not do this. even though eventually ill move this database to a better schema in which the timestamps are autogenerated.
is there a different way around this error? No matter what i seem to do the sql insert query always seems to tack on the +00:00
to the end, is this some kind of default value of using the DataType DATE
? is there a different data type i should be using?
Solution 1:[1]
So I figured out that the previous team likely wasn't storing an actual datetime
timestamp, because once i changed the DataType
from DATE
to STRING
it worked just fine, i was no longer receiving this error, but it still strikes me as strange, because using my DB exporer (i use Razor) when i was having the table described
to me, it clearly stated that these fields where of datetime
and not varchar
, so im not sure why this works, only that it follows the appropriate pattern and it does indeed work.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Austin Howard |