0

I am trying to display the date from time stamp using JavaScript but not working please check my code and it's not working due to string time but if i passed in number then it's working but this time coming from API so i must need to do something here. can anyone please help me.

var timestamp = '1607110465663'
var date = new Date(timestamp);
console.log(date.getTime())
3
  • Pass timestamp as number instead of a string. var timestamp = 1607110465663;. Commented May 12, 2022 at 10:50
  • you are right but i got this from API response so how can i convert it to string Commented May 12, 2022 at 10:55
  • You can use new Date(~~timestamp); or new Date(+timestamp);. There are many possibilities. Regarding maintainability, I would go for new Date(Number(timestamp)); Commented May 12, 2022 at 10:57

3 Answers 3

1

You can convert string to number using methods such as Number or parseInt before creating Date object.

e.g.

var timestamp = '1607110465663'
var date = new Date(Number(timestamp));
console.log(date.getTime())
Sign up to request clarification or add additional context in comments.

Comments

0

Parse string into int then...:

var timestamp = '1607110465663'
var date = new Date(parseInt(timestamp));

and use date object

console.log(date)

Comments

0

Remove the '' from timestamp

var timestamp = 1607110465663
var date = new Date(timestamp);
console.log(date.getTime())
console.log(date)

See JSfiddle

1 Comment

but i received the date in string format so i must need to convert any how in number format

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.