Reputation: 467
I need to convert json date time to string and write it html table cell. Json I am getting is like this:
"creation": {
"date": {
"year": 2022,
"month": 1,
"day": 9
},
"time": {
"hour": 10,
"minute": 14,
"second": 11,
"nano": 830000000
}
I want to display it like this : 1-9-2022 10:14:11:83000000 Is there built-in function in JS. Your help appreciated
Upvotes: 1
Views: 163
Reputation: 850
You can create your own function using template literals, check the doc https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Template_literals
const creation = {
date: {
year: 2022,
month: 1,
day: 9
},
time: {
hour: 10,
minute: 14,
second: 11,
nano: 830000000
}
}
const renderTimestanp = ({date, time}) => `${date.month}-${date.day}-${date.year} ${time.hour}:${time.minute}:${time.second}:${time.nano}`
// will return 1-9-2022 10:14:11:83000000
renderTimestanp(creation)
Upvotes: 1
Reputation: 43507
There is no such build in function, but simple string concatenation will work:
var time = {
"creation": {
"date": {
"year": 2022,
"month": 1,
"day": 9
},
"time": {
"hour": 10,
"minute": 14,
"second": 11,
"nano": 830000000
}
}
};
console.log(
'' + time.creation.date.month + '-' + time.creation.date.day + '-' + time.creation.date.year +
' ' +
time.creation.time.hour + ':' + time.creation.time.minute + ':' + time.creation.time.second + ':' + time.creation.time.nano
);
Upvotes: 0