Simeon Kolev
Simeon Kolev

Reputation: 646

Javascript UNIX time conversion

Hi I have the following problem I am converting this UNIX timestamp to javascript string for date: here is the jsfiddle for it http://jsfiddle.net/tczeU/ and as everyone can see the date is 2 6 2013 13:15:44 so the problem is that this number 1373969744 in UNIX timestamp converter is Tue, 16 Jul 2013 10:15:44 GMT The problem are that there are 14 days bwtween the two dates where does I go wrong? Please help me to convert this date. The code is as in the fiddle:

var date = new Date(1373969744*1000);
var hours = date.getHours();
var minutes = date.getMinutes();
var seconds = date.getSeconds();
var year = date.getFullYear();
var day = date.getDay();
var month = date.getMonth();
var string =day + " " + month + " " + year + " " + hours + ':' + minutes + ':' + seconds;
$("#view").html(string);

and the html:

<div id="view"></div>

So no errors there. Please help. Any help will be apreciated!

Upvotes: 1

Views: 1599

Answers (1)

Brian
Brian

Reputation: 3334

You are using the wrong function to get the day of the month. You are using the function that returns the day of the week, hence the 2 since it's a Tuesday. Check out http://www.w3schools.com/jsref/jsref_obj_date.asp

You need to change .getDay to .getDate and it will work just fine. Or at least it did for me using your jsFiddle link.

Also, don't forget to add one to your month so it has Jul as 7th month instead of 6th like you have it now.

Upvotes: 1

Related Questions