Reputation: 543
I retrieve a date variable from a JSON object in a string format, but I am having trouble getting the elapsed time until now.
import React from 'react';
export default class Date extends React.Component {
render() {
console.log(this.props.date); //shows: 2017-01-25T10:18:18Z
var date = Date.parse(this.props.date.toString());
console.log(date.getTime());
return (
<div >
</div>
);
}
}
Upvotes: 9
Views: 97061
Reputation: 1262
let timeStamp = Date.parse("14 Oct 2022");
console.log(timeStamp);
// Create a new JavaScript Date object based on the timestamp
// If require milliseconds then multiplied by 1000 so that the argument is in milliseconds.
var date = new Date(timeStamp);
var months = ['Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec'];
var year = date.getFullYear();
var month = months[date.getMonth()];
var dateVal = date.getDate();
var formattedDate = dateVal + '/' + (date.getMonth()+1) + '/' + year;
console.log(formattedDate); --> 14/10/2022
Upvotes: 0
Reputation: 2304
If it is not a date object, just create a new date object.
var date = new Date(this.props.date);
var elapsed = date.getTime(); // Elapsed time in MS
If it is a date object, just call this.props.date.getTime()
Upvotes: 12