This question already has answers here:
How to calculate number of days between two dates?
(42 answers)
Closed 3 years ago.
So,
I have this
10 Dec, 2019T14:07:21
format of date coming from backend , what I need is to find how many days ago.
like today is 20 , so for todays date it will give 0 days ago.
You can try this code.
var date1 = new Date("12/13/2010");
var date2 = new Date("12/20/2010");
var diffDays = parseInt((date2 - date1) / (1000 * 60 * 60 * 24), 10);
console.log(diffDays )
You can compare two dates. Substracting them will give you the miliseconds difference. THose miliseconds can be converted into days.
const now = new Date();
// Mimick a backend date
const daysAgo = new Date();
daysAgo.setDate(daysAgo.getDate() - 10);
// Compare both, outputs in miliseconds
const diffMs = now - daysAgo;
// Get the number of days by dividing by the miliseconds in a single day
const daysDiff = Math.round(diffMs/(1000*60*60*24));
console.log(daysDiff)
(new Date()-new Date("10 Dec, 2019T14:07:21".replace("T"," ")))/1000/60/60/24
What you try to do is called "date diff" meaning you want to find the difference in days between 2 dates. First of all you need to create a new Date object from the string you want. You can do that using Moment.js library that will parse your date string and return a Date object.
var date1 = moment("10 Dec, 2019T14:07:21", "DD MMM, YYYY");
var date2 = new Date() //today date
A simple js function accomplishing that is the one below
function dateDiff(date1, date2) {
var datediff = date1.getTime() - date2.getTime();
return (datediff / (24*60*60*1000));
}
dateDiff function will return the difference between dates, so if date1 is a previous day, it will return a negative number of days. In order to convert it to the "x days ago" format you need, you need to simply multiply the result by -1.
Related
I have two iso date and I want to calculate the day difference between two iso dates.I am unable to get the proper day difference.I want to calculate days.
date1=2020-08-14T09:10:49Z
date2=2019-08-06T09:10:49Z
daydifference=date1-date2;
You could achieve this by calculate the difference in millisecond between two date then divide by 1 day (also in millisecond)
Below snippet could help you
const date1 = '2020-08-14T09:10:49Z'
const date2 = '2019-08-06T09:10:49Z'
const DAY_UNIT_IN_MILLISECONDS = 24 * 3600 * 1000
const diffInMilliseconds = new Date(date1).getTime() - new Date(date2).getTime()
const diffInDays = diffInMilliseconds / DAY_UNIT_IN_MILLISECONDS
console.log(diffInDays, 'day(s)')
This question already has answers here:
How to calculate date difference in JavaScript? [duplicate]
(24 answers)
Closed 4 years ago.
I need the difference between two dates in javascript, this is my date format 24-05-2018, and I need no of year, no of months, no of days
Use moment.js
It is much easy with this.
var date1 = moment('24-05-2018','DD-MM-YYYY');
var date2 = moment('24-05-2019','DD-MM-YYYY');
var years = date2.diff(date1, 'year');
date1.add(years, 'years');
var months = date2.diff(date1, 'months');
date1.add(months, 'months');
var days = date2.diff(date1, 'days');
date1 = new Date(date1.getUTCFullYear(), date1.getUTCMonth(), date1.getUTCDate(), date1.getUTCHours(), date1.getUTCMinutes(), date1.getUTCSeconds());
date2 = new Date(date2.getUTCFullYear(), date2.getUTCMonth(), date2.getUTCDate(), date2.getUTCHours(), date2.getUTCMinutes(), date2.getUTCSeconds());
let diff_ms = date2.getTime() - date1.getTime();
let diff = Math.round(diff_ms / (1000 * 60 * 60 * 24));
This will give you difference in no of days. You can then deduce year, month, days.
This question already has answers here:
How to add number of days to today's date? [duplicate]
(16 answers)
Closed 8 years ago.
i have the date 2013-12-28 and i want add one or more more day to it. so if i add one more day it will be 2013-12-29.
i try to add it by adding the value of it's date (date 28+1), it works, but what if i add 7 more day to it? the date will be 35, and of course it is not a valid date format.
can someone help me?
here's the example of my script:
var d = new Date();
var Y = d.getFullYear();
var M = d.getMonth()+1;
var D = d.getDate();
var DT = d.getDate()+1;// what if i + 20 days from today? the format would be invalid
var today = Y+"-"+M+"-"+D;
var tomorrow = Y+"-"+M+"-"+DT;
alert(today+" <> "+tomorrow);
// "<>" means nothing
You may try like this using getdate(), setdate() and getdate():
var myDate = new Date();
myDate.setDate(myDate.getDate() + 7);
If you already have a date object as in the code you show:
var d = new Date();
...then you can add 7 days to it like this:
d.setDate( d.getDate() + 7 );
...and it will automatically increment the month if needed.
Further reading:
The Date object
.getDate() method
.setDate() method
If you need to extract the year, month and day in order to format the result a particular way do so after adding days.
The solution is to convert your date string into unix timestamp, and them add 3600 * 24 * <number of days> to the timestamp and them convert it back to date string.
The code can be as follows:
function addDaysToDate(date, days) {
var time = Date.parse(date) + days * 24 * 3600;
date = new Date(time);
return date.getFullYear() + '-' + date.getMonth() + '-' + date.getDate();
}
var date = '2013-12-28';
console.log(addDaysToDate(date, 7));
I can't seem to get the below code to work properly. While it calculates the difference in the dates, it doesnt account for the years, which throws it way off. The below code returns a difference of only 4 days and ignores the years?
How can this be fixed?
<!DOCTYPE html>
<html>
<head>
<script type="text/javascript">
function test() {
var today = "27/11/2013"
today = new Date(today.split('/')[2],today.split('/')[1],today.split('/')[0]);
var date2 = "23/02/2011"
date2 = new Date(date2.split('/')[2],date2.split('/')[1],date2.split('/')[0]);
var diff = today.getDate() - date2.getDate()
alert(diff)
}
</script>
</head>
<body>
<input type="button" value="test date" onclick="test()"/>
</body>
</html>
You're using the .getDate() method which returns the day of the month so you are only comparing the days of that particular month against one another. Use the getTime() method to return a millisecond representation of the date which can be used for comparison. Also remember that when using JavaScript months you need to -1 when creating a date as 0 is January and 11 is December. This goes the other way as well if constructing a Date string manually from a Date object, you'll need to remember to add 1 to the month.
Here is some code that should compare two dates and return the difference in days.
var today = "27/11/2013"
today = new Date(today.split('/')[2],today.split('/')[1]-1,today.split('/')[0]);
var date2 = "23/02/2011"
date2 = new Date(date2.split('/')[2],date2.split('/')[1]-1,date2.split('/')[0]);
var timeDiff = Math.abs(date2.getTime() - today.getTime());
var diffDays = Math.ceil(timeDiff / (1000 * 3600 * 24));
alert(diffDays); //1008
You create two date objects, and compare their millisecond representations using .getTime() and then convert this back to days by dividing it back up.
Here is a fiddle
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date
You're using getDate(), which returns a day of the month, that's why it's ignoring the year. Just compare Dates like this instead:
var diff = today - date2;
diff will then contain the difference in milliseconds, which you can do with what you please.
getDate() Returns the day of the month (from 1-31)
You might need to use getDate() , getMonth(), and getFullYear() individually. So maybe something like:
var day = today.getDate() - date2.getDate()
var month = today.getMonth() - date2.getMonth()
var year = today.getFullYear() - date2.getFullYear()
alert(month + "/" + day + "/" + year)
I have 2 <input type="text"> tags and I enter dates in them like this 22-05-2013
I want to subtract 22-06-2012 from that date
How do I do this?
I've tried this code but it didn't work:
function returnDate(nDays){
var now = new Date();
var dayOfTheWeek = now.getDay();
now.setTime(now.getTime() - nDays * 24 * 60 * 60 * 1000);
alert(now); // returns current date
alert(now.getFullYear() + "/"+(now.getMonth()+1)+"/"+now.getDate()) // returns new calculated date
}
So I need the difference between 22-05-2013 and 22-06-2012
The best way to do it would be to use Moment.js. It has fantastic support for dates.
In javascript you could subtract 2 Date objects and this will return the number of milliseconds between them. For example:
var date1 = new Date('2013-06-22 01:00:00');
var date2 = new Date('2013-06-22 01:00:01');
var result = date2 - date1;
alert(result); // shows 1000 which is 1 second
Simple:
new Date(mydate1 - mydate2).getDay() ;
This will give you the number of days.
I would suggest you use Date.js though, which would be even simpler.