0

In my web application, I show the user the request posted date time in the view.

I want to additionally show how long ago that was posted with Date time now.

What is the easiest way to show it

This is the Code

<div class="timeline">
  <div class="time-label">
    <span class="bg-green">
      <label id="dateRequested">@Model.First().Approved_Date</label>
    </span>
  </div>
  <div>
    <i class="fas fa-envelope bg-blue"></i>
    <div class="timeline-item">
      <span class="time">
        <i class="fas fa-clock"></i>
      </span>
    </div>
    <i class="fas fa-user bg-green"></i>
    <div class="timeline-item">
      <span class="time">
        <i class="fas fa-clock"></i> 5 mins ago </span>
      <h3 class="timeline-header no-border">
        <a href="#">Managers</a> Previously Approved By
      </h3>
    </div>
  </div>
</div>

<script type="text/javascript">

    const date = $('#dateRequested');
    console.log(date);
    // Do your operations
    const currentTime = new Date();
    console.log(currentTime);
    const seconds = (currentTime.getTime() - date.getTime()) / 1000;
    console.log(seconds);


</script>

@Model.First().Approved_Date (this shows as (12/23/2021 12:10:11 PM)) here shows the posted date and I want to get the difference between that date with Date time now and show the difference in the 5 mins ago place. Can I get help to create this? Thanks

Dev Beginner
  • 589
  • 1
  • 11
  • `@Model.First().Approved_Date` (this shows as (12/23/2021 12:10:11 PM)) it does? In JavaScript/jQuery? Can you post any JS/jQ that you've already tried? Please read [ask] and [mcve]. – zer00ne Jan 29 '22 at 06:16

1 Answers1

0
const date = 'date that you have';
// Do your operations
const currentTime   = new Date();
const seconds = (currentTime.getTime() - date.getTime()) / 1000;

This will give you the seconds from the time you started til now. Now what you can do is that you can convert the seconds into minutes, hours or whatever you want.

Hope that this helped you solve your problem.

Arun Bohra
  • 104
  • 1
  • 10