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?

0

2 Answers 2

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;
    }
Sign up to request clarification or add additional context in comments.

2 Comments

This is what I'd expect to work. Waiting for Rival's comment on this but you will likely get my upvote
No, it's not working with using {convertTimestamp(!dt.timestamp)};
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;
}

1 Comment

Then you'd better remove the convertTimestamp from the innerText of the td but keep the {!dt.timestamp}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.