Reputation: 45
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 .
Upvotes: 0
Views: 854
Reputation: 37775
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>
Upvotes: 0