0

I have my MySQL database inserting timestamp when I upload a record, so what's entered is something like 2020-04-02 16:59:29. Is there a Vue.Js way to convert that into something like 10 Days Ago? If so, can anyone give the code for conversion in Vue.Js?

I hold the fetched DB records in an object called data_local as in my bellow code.

Last Activity : {{data_local.updated_at}} days ago

1 Answers1

0

Use moment.js, you can get your desired data the way you want. First do install moment and then import & use.
Below are ways to get days:
Ex-1:( This will provide you No_of_Days days ago ( e.g- 10 days ago)

moment("2020-04-02 16:59:29").fromNow()

Ex-2: (If you want only 10 days without ago then use below)

moment("2020-04-02 16:59:29").fromNow(true)


For more information visit https://momentjs.com/

Dcoder14
  • 1,831
  • 3
  • 12
  • 22
  • Thanks. The answer was useful. – surhya narayan May 05 '20 at 13:53
  • What are all the possible ways that can be done when there is no timestamp entry in the table field, it is showing invalid date. Please help in this. @Dcoder – surhya narayan May 25 '20 at 13:54
  • simple, do as `moment("2020-04-02", "YYYY-MM-DD")` then depending upon ur requirement you can followed by `format()` or `fromNow()` @surhyanarayan – Dcoder14 May 25 '20 at 14:43
  • I cant understand what you are saying. Can you explain in detail.@Dcoder – surhya narayan May 25 '20 at 15:21
  • what is ur requirement? – Dcoder14 May 25 '20 at 19:59
  • My timestamp fields will be a null value at times, when it is null, the above said method displays invalid date in my UI when the moment function is called. I dont want anything to be shown in this case or it may show any customized text, say date not available. @Dcoder – surhya narayan May 26 '20 at 06:45