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... Commented Oct 29, 2011 at 20:50
  • Why isn't new Date() suitable? 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 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());
}
Sign up to request clarification or add additional context in comments.

4 Comments

Thanks but this shows the current time, and not a fixed timestamp.
You can convert a fixed timestamp from it's milliseconds-since-epoch representation by calling getPastTimestamp(YOURTIMESTAMP)...
This does not work correctly. I get 03/11/2016/09/38/00/ even tho today is 04/11/2016/09/38/00/
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.

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.