最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Convert String to Date in React - Stack Overflow

programmeradmin4浏览0评论

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>
    );
   }
}

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>
    );
   }
}
Share Improve this question edited Jan 25, 2017 at 14:38 mplungjan 178k28 gold badges180 silver badges240 bronze badges asked Jan 25, 2017 at 14:33 Nedjim DNNedjim DN 5432 gold badges6 silver badges12 bronze badges 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. – maniak1982 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). – Frédéric Hamidi Commented Jan 25, 2017 at 14:39
  • Oui effectivement! Merci pour la remarque, je saurai pour la prochaine fois – Nedjim DN Commented Jan 25, 2017 at 14:43
Add a comment  | 

2 Answers 2

Reset to default 12

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

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
发布评论

评论列表(0)

  1. 暂无评论