This question already has answers here:
Date difference in Javascript (ignoring time of day)
(15 answers)
How to calculate number of day name between 2 dates in javascript
(1 answer)
Closed 6 years ago.
I am trying to calculate the number of days since a given date.
today = new Date();
time = today - mydate;
days = Math.floor(time / (1000*60*60*24));
This returns the correct value only if mydate is set to a time before the current time. In other words, if mydate is yesterday at 10 pm and I run this in the morning, I get 0 when I want 1.
Related
This question already has answers here:
JavaScript - get the first day of the week from current date
(20 answers)
Closed 4 years ago.
$gte: ISODate("2010-04-29T00:00:00.000Z"),$lt: ISODate("2010-05-01T00:00:00.000Z")
You may get the day of the week by calling .getDay() on the date object. Once you have the day of the week, rest are arithmetic operations.
To convert Date to ISODateFormat you can use toISOString
var dateForCalculation = new Date();
var prevSunday = new Date(dateForCalculation.setDate(dateForCalculation.getDate()-dateForCalculation.getDay())).toISOString();
var nextSunday = new Date(dateForCalculation.setDate(dateForCalculation.getDate()+7)).toISOString();
console.log(prevSunday);
console.log(nextSunday);
This question already has answers here:
How do I calculate how many seconds between two dates?
(10 answers)
Getting the difference between 2 dates in Javascript in hours, minutes, seconds with UTC
(2 answers)
Get time difference between two dates in seconds
(10 answers)
How to get time difference between two timestamps in seconds with jQuery?
(5 answers)
Javascript return number of days,hours,minutes,seconds between two dates
(24 answers)
Closed 4 years ago.
I Need to subtract These datetime in js
2018-08-05 20:38:02 // start date
2018-08-17 00:00:00 // end date
So that I can later on create a Countdown which tells me the days Hours minutes and second left between These two Dates.
Currently I am not able to correctly subtract those two datetimes, any ideas?
This question already has answers here:
Javascript code for showing yesterday's date and todays date
(6 answers)
Closed 6 years ago.
I want to plot a graph by fetching the previous seven days using the present day in Javascript.
Which should pass through the tests of Leap year, days of mont(30/31), Year change.
Thank you in advance.
var d = new Date();
var yesterday = d.setDate(d.getDate() - 1);
Simply replace - 1 with - 2, - 3 and so on for the previous seven days.
This question already has answers here:
Add 30 days to date (mm/dd/yy)
(4 answers)
Closed 6 years ago.
I'm using Date.now() to get the current date.
I then need to add 30 days to this.
How can I do this?
Would I just work out how many seconds in 30 days, then add this on?
var d = new Date();
console.log(d);
d.setDate(d.getDate() + 30);
console.log(d);
Date.prototype.getDate returns current date day number.
Date.prototype.setDate set date number of given date to value passed to it.
If value exceeds normal date range of month date extra value will handle needed math and change month (or year if necessary) and shows correct result.
var date = new Date;
date.setDate(date.getDate() + 30);
console.log(new Date, date);
This question already has answers here:
Difference in Months between two dates in JavaScript
(29 answers)
Closed 8 years ago.
I'm needing to write a function that takes two dates (startDate, endDate) calculates the time between them in months or years (depending on an input). When the number of months or years is found, I need to iterate a loop that runs a very simple ajax call (I'm adding a record to Quickbase) for every month OR year between the start and end dates.
I've taken a couple of hacks at this but I'm running into trouble when I try to calculate months (due to the calendar dates (i.e. feb having 28 days etc)... I've calculated the time between the two dates in days, but I need months or years.
Any help would be appreciated!
just to start try
function Noofmonths(date1, date2) {
var Nomonths;
Nomonths= (date2.getFullYear() - date1.getFullYear()) * 12;
Nomonths-= date1.getMonth() + 1;
Nomonths+= date2.getMonth() +1; // we should add + 1 to get correct month number
return Nomonths <= 0 ? 0 : Nomonths;
}
obtain date1 year and date2 year.
get the difference and multiply by 12.
obtain date1 month and date2 month.
get the difference.
add the differences together.