I've a date in this format
Fri Mar 22 2013 23:38:20 GMT+0100 (CET)
I must detect how much time it's elapsed since the date to now
I've availability of pre javascript and jquery
You can try this :
function secondsToString(seconds)
{
var numyears = Math.floor(seconds / 31536000);
var numdays = Math.floor((seconds % 31536000) / 86400);
var numhours = Math.floor(((seconds % 31536000) % 86400) / 3600);
var numminutes = Math.floor((((seconds % 31536000) % 86400) % 3600) / 60);
var numseconds = (((seconds % 31536000) % 86400) % 3600) % 60;
return numyears + " years " + numdays + " days " + numhours + " hours " + numminutes + " minutes " + numseconds + " seconds";
}
var t1= Date.parse('Fri Mar 22 2013 23:38:20 GMT+0100 (CET)');
var t2= new Date()
alert(secondsToString((t2-t1)/1000)) //"0 years 0 days 9 hours 39 minutes 40.98599999999715 seconds"
Related
I'm sorry if something similar might be discussed before, but I really need help with it.
Bellow is the code all I want to do is if the number goes over 24h it should switch to 1 day and 0 h, I can't figure it out if someone can please explain how to do it that would be so kind. I'm using it to calculate minutes and hours and want to have also days calculations if the number is higher then 24h.
Thanks in advance
//minutes to hour converter
function ConvertMinutes(num){
h = Math.floor(num/60);
m = num%60;
return(h + "hours"+" :"+" "+m+"minutes").toString();
}
var input = 68.68
console.log(ConvertMinutes(input));
You just need to divide the num by 1440, which is 24 hours in minutes...
Then you need a condition to display the "x days," when there is a value.
I also suggest you to round the minutes...
;)
//minutes to hour (and days) converter
function ConvertMinutes(num){
d = Math.floor(num/1440); // 60*24
h = Math.floor((num-(d*1440))/60);
m = Math.round(num%60);
if(d>0){
return(d + " days, " + h + " hours, "+m+" minutes");
}else{
return(h + " hours, "+m+" minutes");
}
}
var input1 = 68.68
console.log(ConvertMinutes(input1));
var input2 = 4568.68
console.log(ConvertMinutes(input2));
//minutes to hour converter
function ConvertMinutes(num){
h = Math.floor(num/60);
d = Math.floor(h/24);
h = h - d * 24
m = Math.floor(num%60)
s = ((input - d*24*60 - h*60 -m)*60).toFixed(2)
return('days: '+ d + ', hours: '+ h + ', minutes: ' +m+', seconds: '+s);
}
var input = 4568.68
console.log(ConvertMinutes(input));
By refactoring a bit your code, you could try something like the following:
function ConvertMinutes(num){
days = Math.floor(num/1440);
hours = Math.floor((num%1440)/60);
minutes = (num%1440)%60;
return {
days: days,
hours: hours,
minutes: minutes
};
}
var input = 68.68
console.log(ConvertMinutes(input));
function secondsToString(hours)
{
var seconds = hours * 60 * 60;
var numdays = Math.floor(seconds / 86400);
var numhours = Math.floor((seconds % 86400) / 3600);
var numminutes = Math.floor(((seconds % 86400) % 3600) / 60);
var numseconds = ((seconds % 86400) % 3600) % 60;
return numdays + " days " + numhours + " hours " + numminutes + " minutes " + numseconds + " seconds";
}
I am trying to convert a number of weeks, days and hours to seconds and then convert them back again.
When I convert them back, days and hours are not correct:
var weeks = 3,
days = 5,
hours = 1;
//convert to seconds
sec_in_w = weeks * 604800,
sec_in_d = days * 86400,
secs_in_h = hours * 3600,
secs = sec_in_w + sec_in_d + secs_in_h;
//convert back to weeks, days, and hours
new_w = Math.floor(secs / 604800);
secs -= new_w;
new_d = Math.floor(secs / 86400);
secs -= new_d;
new_h = Math.floor(secs / 3600);
console.log('weeks: ' + new_w);
console.log('days: ' + new_d);
console.log('hour: ' + new_h);
DEMO:
http://codepen.io/anon/pen/avZwBp
//convert back to weeks, days, and hours
new_w = Math.floor(secs / 604800);
secs = secs % 604800;
new_d = Math.floor(secs / 86400);
secs = secs % 86400;
new_h = Math.floor(secs / 3600);
Using Modulus gives the remainder.
You are subtracting the number of weeks and days, not the number of seconds in the weeks or days.
secs -= new_w * 604800;
new_d = Math.floor(secs / 86400);
secs -= new_d * 86400;
You have to subtract the number of weeks and days from the seconds
//convert to seconds
sec_in_w = weeks * 604800,
sec_in_d = days * 86400,
secs_in_h = hours * 3600,
secs = sec_in_w + sec_in_d + secs_in_h;
codepen
Lets see whats happening here:
In the line:
secs = sec_in_w + sec_in_d + secs_in_h;
you are adding the value in seconds of 3 weeks + 5 days + 1 hour, or
secs = 2250000;
Then you devide by 604800, and you get 3 as you should, and then you substract this 3 from the big value of secs :). You can do the math yourself:
(2250000 - 3 ) / 86400 = 26
exactly what you got :) The same for the hours.
The solution is to first convert your numbers back to their representation in seconds.
//convert back to weeks, days, and hours
new_w = Math.floor(secs / 604800);
secs -= new_w * 604800;
new_d = Math.floor(secs / 86400);
secs -= new_d * 86400;
new_h = Math.floor(secs / 3600);
By the way, javascript has solutions for handling dates. If you are doing this for the exercise I said nothing of course :)
I'm trying to display a live time elapsed on a page
So I have this function which calculates the exact time elapsed to the second, and when called, it updates the HTML node with this time. It seems woefully inefficient to use a setInterval to call this function every second to update the display. Can anyone suggest a better solution? Many thanks
$scope.getAge = function(){
var today = new Date();
var birthDate = new Date("1980-05-30T04:00:00");
var bdInMilis = birthDate.getTime();
var todayInMilis = today.getTime();
var timeAlive = todayInMilis - bdInMilis;
timeAlive = timeAlive/1000;
var numyears = Math.floor(timeAlive / 31536000);
var numdays = Math.floor((timeAlive % 31536000) / 86400);
var numhours = Math.floor(((timeAlive % 31536000) % 86400) / 3600);
var numminutes = Math.floor((((timeAlive % 31536000) % 86400) % 3600) / 60);
var numseconds = Math.floor((((timeAlive % 31536000) % 86400) % 3600) % 60);
var ageNode = document.getElementById("age-node");
if(ageNode){
ageNode.innerHTML = ("I am "+numyears+ " years, " + numdays + " days, " + numhours + " hours, " + numminutes + " minutes, " + numseconds + " seconds old (give or take)");
// can't keep doing this calculation every second
//$scope.refreshTime();
}
};
When you want a period of time using setInterval is better then timeouts (check this)
Also you're using angularjs, don't bind your string with innerHTML.
<div>{{age}}</div>
...
$scope.getAge = function(){
var today = new Date();
var birthDate = new Date("1980-05-30T04:00:00");
var bdInMilis = birthDate.getTime();
setInterval(function(){
var todayInMilis = today.getTime();
var timeAlive = todayInMilis - bdInMilis;
timeAlive = timeAlive/1000;
var numyears = Math.floor(timeAlive / 31536000);
var numdays = Math.floor((timeAlive % 31536000) / 86400);
var numhours = Math.floor(((timeAlive % 31536000) % 86400) / 3600);
var numminutes = Math.floor((((timeAlive % 31536000) % 86400) % 3600) / 60);
var numseconds = Math.floor((((timeAlive % 31536000) % 86400) % 3600) % 60);
$scope.age = "I am "+numyears+ " years, " + numdays + " days, " + numhours + " hours, " + numminutes + " minutes, " + numseconds + " seconds old (give or take)";
},1000);
};
I am not able to know the reason, why my countdown timer is not working. Here is my code and jsfiddle link :http://jsfiddle.net/CHC8w/
<div id="idays"></div>
<div id="ihours"></div>
<div id="iminutes"></div>
<div id="iseconds"></div>
<script type="text/javascript">
jQuery(document).ready(function() {
setInterval(function(){
var future = new Date('Mar 28 2014 11:35:14');
var now = new Date('Mar 28 2014 11:05:14');
//var future = new Date("Sep 20 2014 21:15:00 GMT+0200");
//var now = new Date();
var difference = Math.floor((future - now) / 1000);
var seconds = fixIntegers(difference % 60);
difference = Math.floor(difference / 60);
var minutes = fixIntegers(difference % 60);
difference = Math.floor(difference / 60);
var hours = fixIntegers(difference % 24);
difference = Math.floor(difference / 24);
var days = difference;
jQuery("#iseconds").text(seconds + " sec");
jQuery("#iminutes").text(minutes + " min");
jQuery("#ihours").text(hours + " hr");
jQuery("#idays").text(days + " days");
}, 1000);
});
function fixIntegers(integer)
{
if (integer < 0)
integer = 0;
if (integer < 10)
return "0" + integer;
return "" + integer;
}
</script>
Kindly help.
Thanks
var now has fixed datetime value var now = new Date('Mar 28 2014 11:05:14'); during each call to setInterval()
Check this too http://jsfiddle.net/CHC8w/1/
// set the date we're counting down to
var target_date = new Date("Aug 15, 2019").getTime();
// variables for time units
var days, hours, minutes, seconds;
// get tag element
var countdown = document.getElementById("countdown");
// update the tag with id "countdown" every 1 second
setInterval(function () {
// find the amount of "seconds" between now and target
var current_date = new Date().getTime();
var seconds_left = (target_date - current_date) / 1000;
// do some time calculations
days = parseInt(seconds_left / 86400);
seconds_left = seconds_left % 86400;
hours = parseInt(seconds_left / 3600);
seconds_left = seconds_left % 3600;
minutes = parseInt(seconds_left / 60);
seconds = parseInt(seconds_left % 60);
// format countdown string + set tag value
countdown.innerHTML = days + "Days </br> " + hours + "hr</br>"
+ minutes + "min</br>" + seconds + "sec</br>";
}, 1000);
Source : https://mindgrader.com/tutorials/1-how-to-create-a-simple-javascript-countdown-timer.
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
IE JavaScript date parsing error
This code working fine in chrome and firefox but it didn't work in IE and safari. It return NAN in IE and invalid date in safair.
var date = new Date("2012-10-17T08:15:19.500-05:00");
var now = new Date();
var difference = now - date;
document.write( "Date: " + date.toLocaleString() + "<br/>");
document.write( "Now: " + now.toLocaleString() + "<br/>");
document.write( "Difference: " + differenceToString(difference) );
function differenceToString(milliseconds) {
var seconds = milliseconds / 1000;
var numyears = Math.floor(seconds / 31536000);
var numdays = Math.floor((seconds % 31536000) / 86400);
var numhours = Math.floor(((seconds % 31536000) % 86400) / 3600);
var numminutes = Math.floor((((seconds % 31536000) % 86400) % 3600) / 60);
var numseconds = Math.floor((((seconds % 31536000) % 86400) % 3600) % 60);
return numyears + " years, " + numdays + " days, " + numhours + " hours, " + numminutes + " minutes, " + numseconds + " seconds";
}
http://jsfiddle.net/RYS3R/
Any idea would be great help.
Thanks
You forgot the Z before the timezone offset, to get valid JS-parsed date according to the language specification - YYYY-MM-DDTHH:mm:ss.sssZ±hh:mm is the only format it must accept. Try
var date = new Date("2012-10-17T08:15:19.500Z-05:00");
…