Is it possible to convert the date & time format i.e. 2018-05-29T04:12:35Z in the format like 1 hour ago or something like this.
Asked
Active
Viewed 43 times
-4
-
What is the exact result you want from? – Ramesh Rajendran May 29 '18 at 04:53
-
I want something like this...... 12 mins ago or 1 hour ago or 1 week ago accordingly.. – Bhawesh Bhaskar May 29 '18 at 04:55
-
Using [MomentJS](https://momentjs.com/) is recommended here. See [this section](https://momentjs.com/docs/#/displaying/fromnow/) specifically. – 31piy May 29 '18 at 04:56
-
Is it possible?? Of course it is. A little bit of web searching would get you numerous approaches to get a good start point from – charlietfl May 29 '18 at 04:56
-
you need to calculate difference between current timestamp (Date.now()) and your given timestamp and then round it to the time units you need, hours in your example. Rounding can be greedy, so you round to the largest unit of time that fits inside the range. – twinmind May 29 '18 at 04:57
-
Possible duplicate of [How to format time since xxx e.g. “4 minutes ago” similar to Stack Exchange sites](https://stackoverflow.com/questions/3177836/how-to-format-time-since-xxx-e-g-4-minutes-ago-similar-to-stack-exchange-site) – supra28 May 29 '18 at 05:11
2 Answers
0
Try this.
console.log(moment("2018-05-29T04:12:35Z").fromNow());
console.log(moment(new Date()).fromNow());
console.log(moment("2018-05-29T05:12:35Z").fromNow());
<script src="https://cdn.jsdelivr.net/momentjs/2.13.0/moment.min.js"></script>
and you can get more details if you search moment.js from now in google

Ramesh Rajendran
- 37,412
- 45
- 153
- 234