I have been given a Unix formatted timestamp in milliseconds, but I would like to remove the date so I only have the hours/minutes/seconds instead of the date so I can compare the time value to another time value that might have a different date. Is there a way to do this using date-fns?
I know I can get the timestamp as a date with fromUnixTime(timeStamp)
but I can’t figure out how to extract the date only to return just the timestamp.
2
Answers
I'm going to use what Michael posted, but to return a valid JS Unix timestamp in milliseconds, all using functions from
date-fns
:You probably don’t need extra libraries to do this, the built-in
Date
object is pretty versatile (and if you really do, date-fns uses theDate
object under the hood). Given a date object, all you need to do is use.getHours()
,.getMinutes()
, and.getSeconds()
functions. If you have your data in some format other than a JavaScript date object, then just convert it into aDate
object (for the more obvious formats, just passing it tonew Date()
will do). Like this: