I've been trying to convert a UNIX timestamp into a YYYY-MM-DD HH-MM-SS date for a Discord Bot, but to no Avail, is ther any way I could do this properly (Since I've been comparing results with a webpage)
Asked
Active
Viewed 3,514 times
1
-
1[This _couldn't possibly_ be a duplicate.](https://stackoverflow.com/search?q=%5Bjavascript%5D+unix+timestamp+to+date) It is also highly recommend to show "to no avail" attempts and errors / incorrect results. – user2864740 Jan 06 '21 at 00:18
-
I've tried [This Attemp](https://hastebin.com/ahamamuged.js) but it wasn't precise at all. other attemtp just gave me time, instead of a constructed Date – Mateo Elias Jan 06 '21 at 00:34
-
See also [How to format a JavaScript date](https://stackoverflow.com/q/3552461/215552) – Heretic Monkey Jan 06 '21 at 00:39
-
"it wasn't precise at all" Do you mean "accurate" rather than "precise"? Because that code would be precise to the second; you could easily add `getMillisecond()` to add precision. Accuracy depends on what you're expecting, since UNIX timestamps (and JavaScript Dates) are derived from UTC (as the "How to format" link's answers discuss). – Heretic Monkey Jan 06 '21 at 00:42
3 Answers
0
Parsing a UNIX timestamp is as easy as just assigning a number to the Date
constructor.
let unix_timestamp = Date.now();
var date = new Date(unix_timestamp);
console.log(date);
Date.now()
retrieves the current UNIX timestamp
On how to format a date, I believe this question will help.

Spectric
- 30,714
- 6
- 20
- 43
-
Not really how to parse a unix timestamp, but how to format it into a readable time. (UNIX -> Time -> Formated Time) 1491673362160 Being the timestamp in my case – Mateo Elias Jan 06 '21 at 00:29
-
@Mateo That's unlikely to be a UNIX timestamp, since it produces a date in the year 49239. It's more likely to be a JavaScript timestamp, since that would produce a date in the year 2017. But I don't know the use case. – Heretic Monkey Jan 06 '21 at 00:45
0
Found an answer thanks those who contributed
var d = new Date(convertIDtoUnix(ID));
timeStampCon = d.getDate() + '/' + (d.getMonth()+1) + '/' + d.getFullYear() + " " + d.getHours() + ':' + d.getMinutes();

Mateo Elias
- 29
- 1
- 4