user759235
user759235

Reputation: 2207

How to create a timestamp with javascript

I want to use a timestamp as an update indicator(last updated at), so i need a current time, month/day/year/hours/minutes/seconds, but the date() returns an live value. Is there a way to do this?

UPDATE: the idea is like this http://web.student.tuwien.ac.at/~e9125168/javas/jstamp.html (this shows a last modified time, but this is for the document).

The script where i need to show a 'last updated on' time is for an jquery ajax script, which updates a certain piece of code every ... seconds/minutes.

Upvotes: 0

Views: 3686

Answers (1)

Daniel Teichman
Daniel Teichman

Reputation: 610

function getPastTimestamp(t) {
    var d = new Date(t);
    var output = "";
    var items = new Array();
    var i = 0;
    items[i++] = d.getMonth() + 1;
    items[i++] = d.getDate();
    items[i++] = d.getFullYear();
    items[i++] = d.getHours();
    items[i++] = d.getMinutes();
    items[i] = d.getSeconds();

    for (i = 0; i < items.length; i += 1) {
        output += (items[i] < 10) ? "0" + items[i] : items[i];
        if (i < items.length - 1) output += '/';
    }

    return output;
}

function getCurrentTimestamp() {
    return getPastTimestamp((new Date()).getTime());
}

Upvotes: 3

Related Questions