user1492785
user1492785

Reputation: 35

JavaScript (Jquery) Decimal Point Removal

Currently I am working on JavaScript, jQuery, HTML5 to improve myself. I have a opensourcely coded clock, which I have converted it into a counter (reverse counter).

Problem I am having is, in my setInterval(){...} I have four variables -> second,min,hour, and day. The problem is, when I get the seconds, I get something like 1.155, 2.312, 3.412 (seconds).

My setInterval function is below

setInterval(function(){
//var duration = parseInt(Date.now() /1000 ) - 1365470000;


var futureTime = Date.parse('April 10, 2013 22:00:00');

var duration = (( parseInt(Date.now() - futureTime ) / 1000));


var seconds = duration % 60;
duration = parseInt(duration / 60);
var minutes = duration % 60;
duration = parseInt(duration / 60);
var hours = (duration)%24;
duration = parseInt(duration / 24);
var days = duration % 365;  


        animation(gVars.green, seconds, 60);
        animation(gVars.blue, minutes, 60);
        animation(gVars.orange, hours, 24);
        animation(gVars.red, days, 365);

    },1000);

}

And my output is below for some random time since i use parseInt(Date.now()).

I have to give the link since I don't have enough rep.

https://i.sstatic.net/0Zkbi.png

How can I get rid of the decimal point in setInterval(){} functions?

Thanks in advance.

Upvotes: 1

Views: 269

Answers (2)

Vadim
Vadim

Reputation: 8779

JavaScript offers more convinient API to work with date and time in order to fetch seconds, minutes, hours and days. Try this code:

var duration,
    seconds,
    minutes,
    hours;

duration = new Date((new Date('April 11, 2013 23:00:00')) - (new Date()));
seconds = duration.getSeconds();
minutes = duration.getMinutes();
hours = duration.getHours();

Now you will have integer values in all 4 variables above, without any decimal point.

Upvotes: 1

user1467267
user1467267

Reputation:

var seconds = 1234.13;
var seconds = seconds + '';

seconds = seconds.split('.')[0];

console.log(seconds);

Upvotes: 1

Related Questions