Nestjs How can I convert date to Timestamp? (I have tried many methods but it doesn't work, please help me)
This is the data type that I return now:
{
"data": {
"results": [
{
"id": 1,
"create_time": "2021-02-27T03:04:06.240Z"
}
]
}
I hope to convert all dates into the following format
{
"data": {
"results": [
{
"id": 1,
"create_time": "1614395046"
}
]
}
Although you can try to use the following methods to achieve, but I don't want to do this, I hope there is a more elegant solution
// Date.prototype.toJSON = (key) => {
// return moment(key).unix().toString()
// };
ps:
Neither foreach nor gettime is what I want. How do I handle this data type uniformly in Nest
In native JavaScript you can use the Date
constructor to get an epoch in milliseconds.
new Date('2021-02-27T03:04:06.240Z').getTime();
given your JSON, simply manipulate it to change the date to a timestamp: and it looks like you have a number of elements in your JSON so:
var myDate = new Date();
for(var i=0;i<obj.data.results;i++){
obj.data.results[i].create_time = myDate(obj.data.results[i].create_time).getTime();
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.