-1

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.

3 Answers3

2

Moment.js is the JavaScript library for all your date and time manipulation needs.

moment.unix(unixTimestampInSeconds).format('D-M-Y')
Thomas
  • 174,939
  • 50
  • 355
  • 478
0

Day.js should fulfill your requirements

Andrzej T
  • 380
  • 2
  • 7
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