0

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.

3
  • 2
    What do you mean by live value? As far as I know, new Date().toString() should work just fine...
    – JCOC611
    Commented Oct 29, 2011 at 20:50
  • Why isn't new Date() suitable?
    – James
    Commented Oct 29, 2011 at 20:50
  • well if i use the getSeconds() if gives me an live value and not a fixed value, so if i want to place a line with the last update at: i will get a current value
    – user759235
    Commented Oct 29, 2011 at 20:56

1 Answer 1

3
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());
}
4
  • Thanks but this shows the current time, and not a fixed timestamp.
    – user759235
    Commented Oct 30, 2011 at 9:07
  • You can convert a fixed timestamp from it's milliseconds-since-epoch representation by calling getPastTimestamp(YOURTIMESTAMP)... Commented Oct 31, 2011 at 1:03
  • This does not work correctly. I get 03/11/2016/09/38/00/ even tho today is 04/11/2016/09/38/00/
    – Black
    Commented Apr 11, 2016 at 7:40
  • I've changed the code to display the calendar month instead of the number of months that have passed since Jan 1 of the year of the timestamp. Commented May 11, 2016 at 23:06

Not the answer you're looking for? Browse other questions tagged or ask your own question.