0

how to convert a ms timestamp to sek.

var timestamp_ms = 1599051221000

how can I convert this timestamp_ms to get DD:MM:YYYY - HH:MM:SS

I tried new Date(timestamp_ms) but it didn't work.

Eden1998
  • 119
  • 1
  • 11

2 Answers2

1

There are many ways to achieve it moment js is a best library among all to play with date and time.

You should try that.

var timestamp_ms = 1599051221000

moment(timestamp_ms).format('DD:MM:YYYY - HH:MM:SS');

OR without any library simple JS

You are already a half way through new Date(timestamp_ms) all you need to store the response in a date variable and play around with JS date docs

var timestamp_ms = 1599051221000

var date = new Date(timestamp_ms)

// date.getHours()
// date.getMinutes()
// date.getSeconds()
// ..... find further date methods in above linked js date docs.
Kamran Khatti
  • 3,754
  • 1
  • 20
  • 31
1

Without additional librarys just pure JavaScript.

var timestamp_ms = 1599051221000
var date = new Date(timestamp_ms);

let result = ('0' + date.getDate()).slice(-2) + ':' +  ('0' + (date.getMonth()+1)).slice(-2) + ':' +  date.getFullYear() + ' ' + ('0' + date.getHours()).slice(-2) + ':' + ('0' + date.getMinutes()).slice(-2) + ':' +  ('0' + date.getSeconds()).slice(-2);

console.log(result);
Sascha
  • 4,576
  • 3
  • 13
  • 34
  • what kind of datatype is timestamp_ms? in my case i declared it as date and the result is aN:aN:NaN aN:aN:aN – Eden1998 Sep 03 '20 at 06:03
  • It's of type number. I just take it over from your question: `var timestamp_ms = 1599051221000`. – Sascha Sep 03 '20 at 09:51