Add a defined amount to a value each day in JavaScript - javascript

I've a problem:
I have a website which should display a number in a specific div.
Now I want the number to increase by a specific amount every day, I am only allowed to use JavaScript and it should return the final number.
For example:
Today the number (start) is 100. Tomorrow it should be 110, the next day 120,... so the function should add 10 to the number each day.
I thought of a kind of loop but this would only work when I reload the site and wait a day :P Is there another option?
Thanks!

You may need to calibrate this a bit, but this should get you going
var startDate = new Date('2-10-2016');
var today = new Date();
var diff = Math.floor((today - startDate)/(1000*60*60*24))
var answer = 100 + diff * 10

If you know the number N that should be added each day, you can actually implement do, in pseudocode:
numberOfPassedDays = Today - initDay
resultToShow = N * numberOfPassedDays + numberAtInit
and calculate it every refresh of the page (in your example N=10 )

Related

Need help subtracting time and date with Google SCRIPT

I have this code here:
var date = a.created_at_timestamp.substring(0,10)
var time = a.created_at_timestamp.substring(11,19)
And both these values return strings with these values:
date = 2020-05-19 //
Time = 17:00:08
I need to subtract 3 hours since it's coming in GMT time and I'm on GMT-3. Therefore, I thought about adding them together, subtracting three hours, and putting them apart again. Something like:
Orig Date: 20/05/19 //
Orig Time: 20:15:19
Time + Date: 20/05/19 20:15 //
Time + Date - 3h: 20/05/19 17:15
New Date: 20/05/19 00:00 //
New Time: 17:15:19
I tried converting it to milliseconds as suggested in other post here, doing with formulas, where a function would trigger formulas adding both cells, which I was able to do, but couldn't tear them apart together. In addition, if possible, I'd like to do it inside the script.
Can someone help me with that?
I'm new at this and I'm somewhat used to VBA. Tried some things from VBA, but they don't really apply here.
Instead of having separate strings for date and time, it'd likely be easier to just create a new Date object with both combined.
var dateTime = new Date(a.created_at_timestamp.substring(0,19));
You can then subtract 3 hours by doing:
var timeOffset = 3 * 60 * 60 * 1000; // 3hrs of millis
var offsetDate = new Date(dateTime.getTime() - timeOffset);

Update value every month using Javascript

everyone!
I was looking for solution of this for a long time and didn't find anything.
I need to make element on page which will have +=2000 every month from ..let's say today's date.
And I am puzzled without any idea how to do this.
I easily wrote updating value every 10 seconds, but what about months that have different length and so on?
Should I compare difference between current date and today's date, than do +=2000*numberOfMonths? Then how often should I check if month has passed no to kill speedload?
Or is there any other convinient way to do it?
I know the solution might be easy, but I don't get it.
Will be gratefull for any suggestions.
You can do it like:
const getMonthsPassed = () => {
const startDate = new Date(2018, 10, 22); // month start at 0
const currentDate = new Date();
const monthDifference =
(currentDate.getFullYear() - startDate.getFullYear()) * 12 +
(currentDate.getMonth() - startDate.getMonth());
return monthDifference;
};

get time different (minutes) in javascript

How do I calculate the difference in minutes given two strings. For example say I have
11:00
11:30
But of course the second string could be 12:11 so I can't subtract just the minutes.
first use javascript to convert the strings to time, then subtract, then convert back to strings
like this:
x = new Date("1/1/01 11:00")
y = new Date("1/1/01 11:30")
// now y-x has difference in milliseconds
// (y-x)/1000 is difference in seconds, etc
The data 1/1/01 is just being used as a dummy value, but the one thing you might have to worry about is are the times on different days, if so you will have to use 1/2/01 for the second time. Unless of course you always know the times are in the same day, but if they can cross "midnight" then you have to adjust for that.
You may want to use http://momentjs.com/ which will take care of the details for you.
When looking for getting metrics such as date , hour , minutes, seconds from the date difference, it's a lot easier to use basic notations as listed here
var x = new Date(new Date().getTime() + 11.5*60*60000); // adds 11 hours - 30 minutes
var y = new Date(new Date().getTime() + 11*60*60000); // adds 11 hours
alert(x.getMinutes() - y.getMinutes()); // gives the difference = 30
Here's an example : https://jsfiddle.net/DinoMyte/157knmgn/

how to calculate number of days between today and given date and code for getTime()?

I want to calculate number of days between today and a given date and check whether how many days remaining until today or how many days past from today.
var today = new Date();
var date_to_reply = new Date('2012-10-15');
var timeinmilisec = today.getTime() - date_to_reply.getTime();
console.log( Math.floor(timeinmilisec / (1000 * 60 * 60 * 24)) );
this gives me 5 as answer but how should i get (-5) since the date_to_reply is 5days past from today?
is this the correct way to calculate any given date?
Regards
What you are doing is correct: You want to calculate the difference (as number of days) between two dates. A difference can't be smaller than zero.
Although your date_to_reply is already in the past, theres still a 5 day difference.
So, everythings fine - it's the correct way.
EDIT:
If you want a negative value as result, try this:
var today = new Date();
var date_to_reply = new Date('2012-10-15');
var timeinmilisec = date_to_reply.getTime() - today.getTime();
console.log( Math.ceil(timeinmilisec / (1000 * 60 * 60 * 24)) );
Remember you need to Math.ceil the final result instead of rounding it down with Math.floor().
If you want the value to be negative (indicating date_to_reply is in the past) you should subtract the past date from the current: date_to_reply.getTime() - today.getTime().
Check this link for ways to calculate more diffentiated results.
If you swap the order of the dates, you'll get the negative number you want.
Better yet you could write a function that does this.
It could subtract the first parameter from the second.
The second parameter could default to today.
function diffDates(dateOne, dateTwo) {
if (typeof dateTwo === 'undefined') {
dateTwo = new Date();
}
return dateOne.getTime() - dateTwo.getTime();
}
It would be better to have the function operate on numbers rather than dates.
That would be more flexible, but I'm typing on an iPad right now!
Its obvious because today's date is greater than the previous. So either you need to make it negative on your own or use this
var timeinmilisec = date_to_reply.getTime()-today.getTime();

How to perform date subtraction in javascript

It's a little complicated to calculate delta time in js.
this is the pseudo-code,
var atime = "2010-12-05T08:03:22Z";
var btime = "2010-01-11T08:01:57Z"
var delta_time = btime - atime;
delta_time ?
I want to know exact date time between two time inputs.
is there any easy way to find out delta time?
var atime = new Date("2010-12-05T08:03:22Z");
var btime = new Date("2010-01-11T08:01:57Z");
var delta_time = btime - atime;
The value of delta_time will be the difference between the two dates in milliseconds.
If you're only interested in the difference, and don't care to differentiate between which is the later date, you might want to do
var delta_time = Math.abs(btime - atime);
A Date / Time object displays a time in a current situation (e.g. now() ). Displaying a difference of time is not part of a Date or Time object because the difference between e.g. May 1 and May 3 would result in, maybe, January 3, 1970, or maybe May 2, depends on how you start counting your delta on.
I would suggest putting your times into a timestamp which is a simple int in seconds. Do some substraction and voilá, there's your delta seconds. This delta can be used to apply to any other Object.

Categories

Resources