Change date on table from monts-days ago to timestamp

Viewed 27

In some internal webpage there is column with "LastBuild" date, but no timestamp there and only something like "X months, Y days ago" or "X days, Y hours ago". There is following HTML code in page source:

<tr id="job-id" class="job-id-class someotherclass-id">
<td data="2021-12-17T06:32:13Z"> == $0
        " 9 mo 10 days -"
<a href="job/agent-info/lastSuccessfulBuild/" class="model-link inside">#2170</a></td>
</td>
</tr>

Is it possible to display this date from " 9 mo 10 days -" to just 2021-12-17T06:32:13Z using JavaScript snippet code?

Don't know is this a good place to ask this question, if not please suggest the correct one.

1 Answers

It seems the value you're after is in the data attribute, so just amend the first text node accordingly.

There are many ways to go about it, here's a trivial example:

function replaceTimestamp() {
  let cell = document.querySelector('td');
  cell.firstChild.data = cell.getAttribute('data') + ' - ';
}
<table>
 <tr id="job-id" class="job-id-class someotherclass-id">
<td data="2021-12-17T06:32:13Z"> 9 mo 10 days -
<a href="job/agent-info/lastSuccessfulBuild/" class="model-link inside">#2170</a></td>
</td>
</tr>
</table>
<br>
<button onclick="replaceTimestamp()">Replace timestamp</button>

Related