A
A
Antoshka2018-03-13 19:23:47
JavaScript
Antoshka, 2018-03-13 19:23:47

How to get number of days from timesamp?

Hello everyone, I'm looking for your help, I can not find it anywhere. We need a script that will output the elapsed number of days from a timesamp value . Thank you.
1 day ago, 2 days ago , 5 days ago , etc.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
T
twobomb, 2018-03-13
@twobomb

function getDays(timestamp){
  var days = Math.floor((Date.now() - timestamp)/(1000*60*60*24));

  if(days % 10 == 1)
    days += " день назад";
   else if(days % 10 == 2 || days % 10 == 3 || days % 10 == 4)
   days += " дня назад";
   else
   days += " дней назад";
   return days;
 }
 alert(getDays(Date.now() - 1000*60*60*24*21));//21 день назад

A
Alexander Pushkarev, 2018-03-13
@AXP-dev

https://momentjs.com/

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question