0

I'm trying to convert this 13 digit Unix timestamp (1563398686957) to YYYYMMDD format using Javascript. How can I do this?

I have divided the 1563398686957/1000 and tried to get the first 10 digits but converting from Number to String and back gives me an error and is not there right way to do it if I am looping for many timestamps.

var newCreateDate = 1563398686957 / 1000;
var newTimestamp = Array();
for (let i = 0; i < newCreateDate.length; i++) {
    temp_timestamp = String(newCreateDate[i].slice(0, 9));
    newTimestamp.push(Number(temp_timestamp));
}
2

2 Answers 2

1

You can pass timestamp into Date:

var unixts = 1563398686957;
var date = new Date(unixts);

var fdate = date.getFullYear() + '/' + ("0" + (date.getMonth() + 1)).slice(-2) + '/' + ("0" + date.getDate()).slice(-2);
console.log(fdate);

Sign up to request clarification or add additional context in comments.

1 Comment

@kshetline true
1

new Date(1563398686957).toISOString().substr(0, 10)

Will give you the date in this form: 2019-07-17

new Date(1563398686957).toISOString().substr(0, 10).replace(/-/g, '/')

Will change the dashes to slashes, if you prefer, and...

new Date(1563398686957).toISOString().substr(0, 10).replace(/-/g, '')

Would give you 20190717.

1 Comment

short and great

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.