# Write date in "weeks ago"

I try to write my `date` of post with the addition of “x weeks ago”.

I try to re-write it from below code, but no success, somehow it calculates my weeks wrong. Instead it is really 5 weeks, it counts 18 weeks…

``````< div class = "date" >
<%= date(post.date, 'MMM D, YYYY') %> & nbsp <%var difdt =new Date(new Date()- post.date);
var resDate='(';
resDate+=(difdt.toISOString().slice(0,4)-1970)? difdt.toISOString().slice(0,4)-1970 +" years ": '';
resDate+=(difdt.getMonth())? difdt.getMonth()+" months " : '';
resDate+=(difdt.getDate()-1)? (difdt.getDate()-1)+" days " : '';%>
<%if(resDate=='('){
resDate+=(difdt.getHours())? difdt.getHours()+" hours " : '';
resDate+=(difdt.getMinutes())? difdt.getMinutes()+" minutes " : '';
}
%>
<%if(resDate){%> <
time >
<%= resDate+" ago)" %> <
/time> <
/div>
``````

Have you considered using milliseconds to figure out the elapsed time instead (in milliseconds) and the converting that value to weeks? It will simplify your calculation a lot.

Algorithm can be something like:
Elapsedtime=newtimeinmilli - oldposttimestampinmilli
Weeks= elapsedtime / 1000 / 60/ 60/ 24/ 7;
Then round and ceil the value to make it a whole number.

@hbar1st

Hi…

I came up with something like this:

(I use EJS code), but it does not work somehow (I decided to go for “days” only).

``````<% var today = new Date (); %>
<% var createdOn = new Date(d.post.event_published_date); %>
<% var msInDay = 24 * 60 * 60 * 1000; %>

<% createdOn.setHours(0,0,0,0); %>
<% today.setHours(0,0,0,0) %>

<% var diff = (+today - +createdOn)/msInDay %>
<%if(diff){%>
<time>
<%= diff+" ago)" %>
</time>
<%}%>
``````

here’s a repl.it javascript that shows I calculate the weeks passed using milliseconds
In this case I picked a test of using august 1, 2018 to compare against today’s date