10

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>
    );
   }
}
3
  • 6
    I think somebody voted it down because it was in French. I don't know if there is a French language version of the site but this particular version is English-only. Commented Jan 25, 2017 at 14:37
  • 3
    Désolé, nous acceptons uniquement les questions en langue anglaise. Apparemment, personne n'a jugé nécessaire de vous informer de cela quand vous avez posé votre première question. Vous voilà donc prévenu(e). Commented Jan 25, 2017 at 14:39
  • Oui effectivement! Merci pour la remarque, je saurai pour la prochaine fois Commented Jan 25, 2017 at 14:43

2 Answers 2

14

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()

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

Comments

0
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

Comments

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.