2

I am able to convert timestamp to datetime from this question:
Convert a Unix timestamp to time in JavaScript

But how to convert it in table? Calling the javascript function within the table row doesnt work

<body>
<table id="resulttable" class="display">
    <thead>
        <td>Date</td>
        <td>Booking ID</td>
        <td>Name</td>
        <td>Phone</td>
        <td>Email</td>
        <td>Amount</td>
        <td>Type</td>
    </thead>
    <tbody>
        <apex:repeat id="repeatdata" var="dt" value="{!resultList}">
            <tr>
                <td>
                    convertTimestamp({!dt.timestamp});
                </td>

                <td>{!dt.bookingId}</td>
                <td>{!dt.name}</td>
                <td>{!dt.phone}</td>
                <td>{!dt.email}</td>
                <td>{!dt.amount}</td>
                <td>{!dt.productType}</td>

            </tr>
        </apex:repeat>
    </tbody>
</table>

javascript:

function addZero(i) {
    if (i < 10) {
        i = "0" + i;
    }
    return i;
}
function convertTimestamp(pTimestamp) {
    var result = new Date(pTimestamp);
    var dd = addZero(result.getDate());
    var mm = addZero(result.getMonth()+1); //January is 0!
    var yyyy = result.getFullYear();
    var HH = addZero(result.getHours());
    var min = addZero(result.getMinutes());
    result = yyyy + '/' + mm + '/' + dd + ' ' + HH + ':' + min;
    return result;
}

Basically, I want to convert timestamp:

convertTimestamp(1488966492914);

to:

2017/03/08 04:48

in all list of record inside the table based on parameter inputted in javascript parameter value.
How can I achieve this?

Rival
  • 173
  • 1
  • 1
  • 9

2 Answers2

1

try it by using convertTimestamp javascript functions within the semi braces like that

<td>
   {convertTimestamp(!dt.timestamp)}; //call javscript function within the braces
</td>

javsscript function to convert timestamp to date

convertTimestamp(date){
        let d = new Date(date);
        var month = (d.getMonth()+1).toString();
        let day = d.getDate().toString();
        let year = d.getFullYear();
        if(month['length'] < 2){
            month = `0${month}`;
        }
        if(day['length'] < 2){
            day = `0${day}`;
        }
        return month + "/" + day + "/" + year;
    }
sainanky
  • 507
  • 4
  • 13
0

You can assign an ID to the TD you want to change.

            <td id="idFromBackEnd_index">
            </td>

And then can convert it through javascript on document ready or any event to your desired form by calling your own function and playing it through the ID.

function convertTimestamp() {
var pTimeStamp = document.getElementById("idFromBackEnd_index").innerText;
var result = new Date(pTimestamp);
var dd = addZero(result.getDate());
var mm = addZero(result.getMonth()+1); //January is 0!
var yyyy = result.getFullYear();
var HH = addZero(result.getHours());
var min = addZero(result.getMinutes());
result = yyyy + '/' + mm + '/' + dd + ' ' + HH + ':' + min;
return result;
}
Farhan Qasim
  • 990
  • 5
  • 18
  • 1
    Then you'd better remove the `convertTimestamp` from the innerText of the `td` but keep the `{!dt.timestamp}` – Rafalon Jan 24 '18 at 11:35