I am newbee in JavaScript and i am having toughtime calculating seconds elapsed in current year not from 1970 as JavaScript default method calculates. Basically i need to develop script which will show countup timer of products produced in current year. Question is a company produces 4 million product in a year and i need to show Counter which will show current number of items produced and keep on counting up as long as i am on this counter page. So i need number of items produced till time i have opened page and numer of milliseconds remaining in current year till 31st dec at 24:00
Asked
Active
Viewed 45 times
-1
-
1What have you tried? How was it deficient? – Scott Hunter Mar 21 '19 at 12:42
4 Answers
1
const first = new Date();
first.setDate(1);
first setMonth(0);
first.setHours(0);
first.setMinutes(0);
const delta = Date.now() - first;
const perMS = 4 * 10 ** 6 / (1000 * 60 * 60 * 24 * 365);
console.log(delta * perMS);

Jonas Wilms
- 132,000
- 20
- 149
- 151
0
This will give you the total number of milliseconds passed in the current year, you can then subtract it from total milliseconds in a year
var diff = new Date(Math.abs(new Date() - new Date(new Date().getFullYear(), 0, 1))).getTime();
console.log(diff);

Chaitanya Bankanhal
- 463
- 4
- 10
-
1Thanks, i used this script and got desired result. Thanks all who replied to my question. – Jagdish Prabhu Mar 21 '19 at 16:34
0
I have used below script, countup script is working fine, I need 'from' and 'speed' variable.
$.fn.countTo = function(options) {
// merge the default plugin settings with the custom options
options = $.extend({}, $.fn.countTo.defaults, options || {});
// how many times to update the value, and how much to increment the value on each update
var loops = Math.ceil(options.speed / options.refreshInterval),
increment = (options.to - options.from) / loops;
return $(this).each(function() {
var _this = this,
loopCount = 0,
value = options.from,
interval = setInterval(updateTimer, options.refreshInterval);
function updateTimer() {
value += increment;
loopCount++;
$(_this).html(value.toFixed(options.decimals));
if (typeof(options.onUpdate) == 'function') {
options.onUpdate.call(_this, value);
}
if (loopCount >= loops) {
clearInterval(interval);
value = options.to;
if (typeof(options.onComplete) == 'function') {
options.onComplete.call(_this, value);
}
}
}
});
};
$.fn.countTo.defaults = {
from: 0, // the number the element should start at
to: 100, // the number the element should end at
speed: 1000, // how long it should take to count between the target numbers
refreshInterval: 100, // how often the element should be updated
decimals: 0, // the number of decimal places to show
onUpdate: null, // callback method for every time the element is updated,
onComplete: null, // callback method for when the element finishes updating
};
var date = new Date("2019, 1, 1");
var seconds = date.getTime() / 1000;
var prRatePerSec = 0.12;
var prPerYear = 4000000;
var prProducedTill = Math.round(seconds * prRatePerSec);
$('.timer').countTo({
from: 0,
to: prPerYear,
speed: 31540000000,
refreshInterval: 50,
onComplete: function (value) {
console.debug(this);
}
});
0
You can substract two date object with javascript.
So substract the current date with the start year date
Like
var second = diffCurrentYear() / 1000;
var day = second / 60 / 60 / 24;
console.log(day)
function diffCurrentYear() {
var currentYear = new Date().getFullYear(),
diff = (new Date()).getTime() - (new Date(currentYear + '-01-01')).getTime()
return diff;
}

R3tep
- 12,512
- 10
- 48
- 75