Is there an npm library for conversion from unix time to day month year? Or, ideally just the time and day and month? I can't really find an easy to use library that I can just port in.
Asked
Active
Viewed 81 times
-1
-
Does this answer your question? [How to generate timestamp unix epoch format nodejs?](https://stackoverflow.com/questions/25250551/how-to-generate-timestamp-unix-epoch-format-nodejs) – xdeepakv Mar 29 '20 at 09:49
-
No, I am taking a unix time and converting it to a usable calendar date, particularly time. I am not using the date library of javascript to convert to UNIX time as the question you submitted proposes. The titles are confusingly similar. – Fifth Dimension Dragon Mar 29 '20 at 10:11
-
npm library is same as javascript. i dont see any differenece. – xdeepakv Mar 29 '20 at 10:12
-
UNIX time => D-M-Y, not D-M-Y => UNIX time. – Fifth Dimension Dragon Apr 01 '20 at 10:37
-
if u looking for native solution, Please check my answer. Thanks! – xdeepakv Apr 01 '20 at 11:01
3 Answers
0
Without third-party lib, simple implement.
let unixTime = 1585737917;
function padStart(num, d) {
d = String(d);
const delta = num - d.length;
if (delta > 0) {
for (let i = 0; i < delta; i++) {
d = "0" + d;
}
}
return d;
}
const pad = padStart.bind(null, 2);
function parseUnixDate() {
const date = new Date(unixTime * 1000);
const [dd, mm, yy] = [
date.getDate(),
date.getMonth() + 1,
date.getFullYear()
];
return `${pad(dd)}-${pad(mm)}-${yy}`;
}
console.log(parseUnixDate(unixTime));

xdeepakv
- 7,835
- 2
- 22
- 32