0

I am looking for to convert this date and time to relative time like twitter have ex. 5 sec ago or 1 month ago. I have stored date and time in different column of database. i am executing this in JavaScript, i tried Momentjs but it does not work well or i may not understand its format or flow of work. i am little new to javascript, i started learning react a week ago from database:

date =>31.03.2019 time=>16:06:05

Thank You For Help.

Success Update : Mission was to convert "31.3.2019 19:45:55" this date into a time like twitter or facebook have.

let date = new Date()

   var then = request.date +' '+ request.time;

 var ms = moment(date,"DD/MM/YYYY HH:mm:ss").diff(moment(then,"DD.MM.YYYY HH:mm:ss"));
  var d = moment.duration(ms);
  var s = Math.floor(d.asHours()) + moment.utc(ms).format(":mm:ss");
  var timeAgo = d.humanize();;

In moment we can define the format of date we are passing and with humanize function its easy to get the desire output. What i didnt know was the definition of format of date we pass. #NOW I AM CLEAR SIR. Thank You For Helping Everybody. Thank You For Sharing .

  • Duplicate of https://stackoverflow.com/questions/18623783 – Magix Mar 31 '19 at 13:32
  • 1
    Possible duplicate of [Get the time difference between two datetimes](https://stackoverflow.com/questions/18623783/get-the-time-difference-between-two-datetimes) – Magix Mar 31 '19 at 13:32
  • What have you achieved so far? Please show your work. – Onur Arı Mar 31 '19 at 13:32
  • var rightNow = (day +'/'+ month +'/'+ year+ ' '+hours+':'+minutes+':'+seconds); console.log(rightNow); // 31/2/2019 19:52:44 var then = "31/03/2019 18:51:44"; console.log(then); //31/03/2019 18:51:44 var ms = moment(rightNow,"DD/MM/YYYY HH:mm:ss").diff(moment(then,"DD/MM/YYYY HH:mm:ss")); var d = moment.duration(ms); var s = Math.floor(d.asHours()) + moment.utc(ms).format(":mm:ss"); console.log(s); //NaNInvalidDate – Rahul Shahare Mar 31 '19 at 14:25
  • var rightNow = (day +'/'+ month +'/'+ year+ ' '+hours+':'+minutes+':'+seconds); console.log(rightNow); // 31/2/2019 19:52:44 var then = "31/03/2019 18:51:44"; console.log(then); //31/03/2019 18:51:44 var ms = moment(rightNow,"DD/MM/YYYY HH:mm:ss").diff(moment(then,"DD/MM/YYYY HH:mm:ss")); var d = moment.duration(ms); var s = Math.floor(d.asHours()) + moment.utc(ms).format(":mm:ss"); console.log(s); //NaNInvalidDate – Rahul Shahare Mar 31 '19 at 14:28
  • @OnurArı so far so now i have did this – Rahul Shahare Mar 31 '19 at 14:41
  • let date = new Date() var then = "27.03.2019 18:51:44"; var ms = moment(date,"DD/MM/YYYY HH:mm:ss").diff(moment(then,"DD.MM.YYYY HH:mm:ss")); var d = moment.duration(ms); var s = Math.floor(d.asHours()) + moment.utc(ms).format(":mm:ss"); console.log(d); //Successfully get the object with value. trying to access the value now. Thank you all. help me to aceess object – Rahul Shahare Mar 31 '19 at 14:42

1 Answers1

0

You can use moment js

let date = new Date()

console.log(moment(date).fromNow())
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.18.1/moment.min.js"></script>
Code Maniac
  • 37,143
  • 5
  • 39
  • 60
  • how do i access the object return by moment – Rahul Shahare Mar 31 '19 at 14:46
  • @RahulShahare what do you mean by this `how do i access the object return by moment` ? you can access as you access any other function in JS and assign return value to any variable you want – Code Maniac Mar 31 '19 at 15:22