0

I have a long numbber in array in javascript. It was taken from getTime(), but now its not an Date object.

tab[0][1]=1373139837555;// not date object - just int

And now I want to print it like this

Hour:Minute:Seconds | Day/Month/Year

Is there any function to print date? I know the functions getHours(), getMinutes(), getTime(), but they only work for Date objects. Can I turn this long number to date? How can I print the date otherwise?

EDIT

This is my code with javascript and JQ

$.date = function (dateObject) {
    var d = new Date(dateObject);
    var hour = d.getHours();
    var minutes = d.getMinutes();
    var seconds = d.getSeconds();

    var day = d.getDate();
    var month = d.getMonth();
    var year = d.getFullYear();

    var date = hour+":"+minutes+":"+seconds+"||"+day+"/"+month+"/"+year;

    return date;
};

tab[0][1]=1373139837555;
console.log('Number '+$.date(tab[0][1]));

And it returns Number NaN:NaN:NaN || NaN/NaN/NaN :( When I chech the tab[0][1] with is NaN it returns false - so it is a number

MateuszC
  • 481
  • 2
  • 5
  • 14
  • 2
    possible duplicate of [Convert a Unix timestamp to time in Javascript](http://stackoverflow.com/questions/847185/convert-a-unix-timestamp-to-time-in-javascript) – JJJ Jul 06 '13 at 20:03
  • @Juhana: Though I voted for closing as well, it's not an exact duplicate: Unix timestamps are in seconds and need to be multiplied by 1000 - a timestamp coming from `getTime` doesn't – Bergi Jul 06 '13 at 20:07
  • Works fine for me. http://jsfiddle.net/46zKR/ (other than the month being off by one). Are you *really* sure the variable contains the number? If you do `console.log(tab[0][1])` you get the timestamp? – JJJ Jul 06 '13 at 20:57

3 Answers3

2

You should be able to create a new date object using your long number (if I am not mistaken, this is the number of milliseconds since 1/1/1970)

tab[0][1]=1373139837555;

var date = new Date(tab[0][1]);

You can then get the day, month, etc using the functions you mention:

var printDate = date.getHours() + ':' + date.getMinutes() + ':' + date.getSeconds() + 
                ' | ' + 
                date.getDate() + '/' + (date.getMonth() + 1) + '/' + date.getFullYear();
Simon Adcock
  • 3,554
  • 3
  • 25
  • 41
2

Convert it to a Date

new Date(1373139837555)
bsoist
  • 775
  • 3
  • 10
1

To convert it to Date, just try this¨

var myDate = new Date(tab[0][1]);

Once is converted, use getHours(), getMinutes(), getTime(), etc.

Antonio MG
  • 20,382
  • 3
  • 43
  • 62