Javascript: Calculate time difference between 2 days with conversion to UTC - javascript

I have a start and end dates, I need to convert them to UTC and calculate how many days are in between (including).
So for example:
(01/08/15 10:00 GMT+3) - (04/08/15 10:00 GMT+3) will return 4
(01/08/15 00:00 GMT+3) - (04/08/15 10:00 GMT+3) will return 5
The following code works for those dates like the first case, but not for the second (where after the conversion there is an additional day):
var startDateInUTC = new Date(start.getUTCFullYear(), start.getUTCMonth(), start.getUTCDate(), start.getUTCHours(), start.getUTCMinutes(), start.getUTCSeconds());
var endDateInUTC = new Date(end.getUTCFullYear(), end.getUTCMonth(), end.getUTCDate(), end.getUTCHours(), end.getUTCMinutes(), end.getUTCSeconds());
var totalDays = Math.floor((endDateInUTC - startDateInUTC) / (1000 * 60 * 60 * 24)) + 1;
I tried changing the Math.floor to Math.round but that just adds me a day in some scenarios.
What am I doing wrong?

function calculate(start, end)
{
var startDateInUTC = new Date(start.getUTCFullYear(), start.getUTCMonth(), start.getUTCDate(), start.getUTCHours(), start.getUTCMinutes(), start.getUTCSeconds());
var endDateInUTC = new Date(end.getUTCFullYear(), end.getUTCMonth(), end.getUTCDate(), end.getUTCHours(), end.getUTCMinutes(), end.getUTCSeconds());
return Math.floor(millisecondsToDays = (Date.parse(endDateInUTC) - Date.parse(startDateInUTC)) / 1000 / 3600 / 24);
}
console.log(calculate(new Date("2015/08/01 10:00:00"), new Date("2015/08/04 10:00:00")));
console.log(calculate(new Date("2015/08/01 00:00:00"), new Date("2015/08/04 10:00:00")));
//the answer in both cases will be 3
Use Date.parse here. It will convert the dates into timeStamps. you can subtract these and then calculate the amount back to days. Use Math.floor to round down, since 6.25 is 6 days and 6 hours.
timeStamps are the amount of milliseconds that have passed since 1970/01/01 00:00:00. That date is always UTC. When you have two timestamps you can calculate the difference between them. Date.parse() returns the timestamp on a valid date. new Date(timestamp) will return the date based upon the timestamp.
To get date barriers you can do an extra calculation:
(start time + 24 * days + end time) / 24
Round this figure down and you get the day barriers.
Example:
21 + 24 * 3 + 7 = 100
103 / 24 = 4.1666666.....
Math.floor(4.166666) = 4;

I ended up gathering a pretty simple solution combining some bits of Mouser's answer (Thanks!)
function calcStreamDaysInUTC(start, end) {
try {
// Translate to UTC
var startDateInUTC = new Date(start.getUTCFullYear(), start.getUTCMonth(), start.getUTCDate(), start.getUTCHours(), start.getUTCMinutes(), start.getUTCSeconds());
var endDateInUTC = new Date(end.getUTCFullYear(), end.getUTCMonth(), end.getUTCDate(), end.getUTCHours(), end.getUTCMinutes(), end.getUTCSeconds());
// Reset everything but the date
startDateInUTC.setHours(0);
startDateInUTC.setMinutes(0);
startDateInUTC.setSeconds(0);
endDateInUTC.setHours(0);
endDateInUTC.setMinutes(0);
endDateInUTC.setSeconds(0);
var totalDays = (endDateInUTC - startDateInUTC) / (1000 * 60 * 60 * 24) + 1;
return totalDays;
} catch (e) {
return -1;
}
}

Related

JavaScript: is there a way to detect that a given date in such a format is within last X days?

I have a bunch of timestamp formatted like this
const dates = ['2021.6.01', '2021.6.11', '2021.9.02']
I wanted to write a util that can tell me if a date with such a format is within the last X days of another date.
For example, 2021.6.10 is within the last 7 days of 2021.6.12 while 2021.6.01 is not within the last 7 days of 2021.6.12.
I was think the API interface would be but please feel free to suggest a better naming
function isWithinTheLastDays(originalDate, date, days)
I found it really tricky to implement by hand and there are a lot of edge cases.
Parse the dates with the Date constructor, subtract the two dates and convert the millisecond difference to days (by dividing by 86400000), then check whether it is smaller or equal to days:
function isWithinTheLastDays(originalDate, date, days){
return (new Date(date) - new Date(originalDate)) / 86400000 <= days;
}
console.log(isWithinTheLastDays('2021.6.10', '2021.6.12', 7)) //2 day diff
console.log(isWithinTheLastDays('2021.6.10', '2021.6.17', 7)) //7 day diff
console.log(isWithinTheLastDays('2021.6.10', '2021.6.18', 7)) //8 day diff
turning it to Unix timestamp and subtracting it
function isWithinTheLastDays(originalDate, date, days){
if(Math.abs(
new Date(date).getTime()/1000
- new Date(originalDate).getTime()/1000
)/60/60/24 >= days){
return true;
}else{
return false;
};
}
You can try this:
Note: this will return true if the dates is X days after and before the date you've set
const dates = ['2021.08.26', '2021.6.11', '2021.9.02']
function isWithinDays({ originalDate, date, range }) {
return parseInt(((new Date(date) - new Date(originalDate)) / (1000 * 60 * 60 * 24)), 10) <= range && parseInt(((new Date(date) - new Date(originalDate)) / (1000 * 60 * 60 * 24)), 10) >= 0 ? true : false
}
for (var i = 0; i < dates.length; i++) {
console.log(isWithinDays({
originalDate: dates[i],
date: new Date('2021-08-27'),
range: 10
}))
}

Javascript Subtract Set Date by Todays Date Within Array

currently I have an array that is pulling a future date, and I'm trying to do the "future date" - "todays date" within an array to get a number. The format that the future date is currently being outputted as is below.
2021-06-21T16:23:26.182Z
I was wondering how I could trim the date when it's being pulled within an array, and then subtract the future date from todays date, to give a number output.
Currently within the array the future date property is being pulled as shown below.
futureDate: machine.future.date
Would I just do the "split" function on the date to trim it down to 2021-06-21? Any help would be much appreciated, thanks!
var futuredate="2021-06-21T16:23:26.182Z";
var dif = new Date(futuredate).setHours(0,0,0,0) - new Date().setHours(0,0,0,0);
// dif is in milliseconds
// days difference = dif / 1000 / 60 / 60 / 24
// years difference = dif / 1000 / 60 / 60 / 24 / 365
To address your comment, you can map one array to another - getting the diff value along the way, like:
var arr = ["2021-06-21T16:23:26.182Z", "2021-06-22T16:23:26.182Z"];
var difarr = arr.map(futuredate => {
return (new Date(futuredate).setHours(0,0,0,0) - new Date().setHours(0,0,0,0)) / 1000 / 60 / 60 / 24 ;
})
//difarr is an array of the difference in days (in this example)

How do I get the number of days for a duration where the number of hours exceeds 24 in google sheets?

I am using google sheets where there is a duration value of 69:41:00 where it's 69 hours, 41 minutes, 0 secs. There doesn't seem to be a function to convert this to days, hours and minutes so I did some searching and some had suggested a custom function. Not sure exactly how it works but made some changes from the original to fit what I needed. The code below:
/**
* Format Duration to Days,Hours,Minutes
*
* #param {duration} input value.
* #return Days,Hours,Minutes.
* #customfunction
*/
function FormatDuration(duration) {
// Retrieve the hours and minutes
var hrs = duration.getHours();
var days = Math.floor(hrs/24);
var hours = hrs % 24;
var mins = duration.getMinutes();
// Convert the result to a number to use in calculations
var result = days + 'd ' + hours + ' h '+ mins+' min';
return result;
}
The result should be 2d 21h 44 min but instead I got 0d 21 h 35 min. Am I doing something wrong here?
I was going to add, why don't you just use a custom format of
ʺd\d hh\h mm\mʺ ?
This works fine in Excel but not in GS because it uses a different base for dates so duration like 69:41:00 would be interpreted as 1/1/1900 21:41 and the days are not correct. So you would have to break it down into days (whole numbers) and hours+minutes (fractions of a day) like this
=text(int(A1),ʺ#0\d ʺ)&text(mod(A1,1),ʺHH\h MM\mʺ)
You can make it work in Google Scripts if you want to by adjusting the date - should work OK for durations up to 1 month.
The reason for adding 2 to the date is that a time like 03:21:00 (less than a day) is seen as a date - namely 30th December 1899 ! So I add 2 to it to make it 1st January 1900. However, now the day part of the date is 1 and I want it to be zero. So I have to subtract 1 from the day further down.
This strange behaviour is probably why you're advised to do it the other way and work in milliseconds, but I was just interested to see if there was a way of making the original code work.
/**
* Format Duration to Days,Hours,Minutes
*
* #param {duration} input value.
* #return Days,Hours,Minutes.
* #customfunction
*/
function FormatDuration(duration) {
// Add 2 days to the date
var date=new Date(duration.setDate(duration.getDate()+2));
Logger.log(date.getDate());
var hours = duration.getHours();
// Take 1 off the day part of the date
var days = date.getDate()-1;
var mins = duration.getMinutes();
// Convert the result to a number to use in calculations
var result = days + 'd ' + hours + ' h '+ mins+' min';
return result;
}
function(durations){
var timeArr = durations.split(':'); //["69","41","00"]
//your code
}
getHours is a method of object Date.
var t = new Date;
t.getHours();
How do you expect to get more than 24hours from a Date object? It is not the same as what you expect as Duration. Date is for points of time in calendar, so at most you'd get the 23:59:59 of any day. You can get date2 - date1 = milliseconds diff, and work on it, as following;
function FormatDuration(date1, date2) {
var milliseconds = date2 - date1;
var mins = Math.floor((milliseconds / (1000*60)) % 60);
var hours = Math.floor((milliseconds / (1000*60*60)) % 24);
var days = Math.floor(milliseconds / (1000*60*60*24));
var result = days + ' d ' + hours + ' h '+ mins + ' min';
console.log(result);
}
FormatDuration(new Date(2000, 5, 1, 5, 13, 0, 0),
new Date(2000, 5, 2, 15, 31, 0, 0))
You can find more details here

How to get the number of weeks from a date in JS?

I need to find the number of weeks passed from a particular month, till date.
Like if its Nov, 2013 (as of today, 10th Jan, 2014) it should return 9 weeks.
Is there a way to find it?
Try the following:
function differenceInWeeks(d1, d2) {
var t2 = d2.getTime();
var t1 = d1.getTime();
return parseInt((t2-t1)/(24*3600*1000*7));
}
The getTime function returns the number of milliseconds since 1970/01/01, the rest is just maths.
Try this:
function weeksSince(dateString){
var date = new Date(dateString);
var today = new Date();
return Math.floor((today-date)/(1000*60*60*24*7));
}
console.log(weeksSince("January 01, 2014"));
console.log(weeksSince("January 01, 2013"));
console.log(weeksSince("January 01, 2012"));
=> 1
=> 53
=> 105
Try This:
function weeks_between(date1, date2) {
// The number of milliseconds in one week
var ONE_WEEK = 1000 * 60 * 60 * 24 * 7;
// Convert both dates to milliseconds
var date1_ms = date1.getTime();
var date2_ms = date2.getTime();
// Calculate the difference in milliseconds
var difference_ms = Math.abs(date1_ms - date2_ms);
// Convert back to weeks and return hole weeks
return Math.floor(difference_ms / ONE_WEEK);
}
If you want them to be very precise(including days and time) then use these jquery libraries for that:
timeago
javascript pretty date
That's a little vague, and the other answers are correct - it's basically just maths once you understand how to get milliseconds out of a date...
Try this fiddle as a start;
http://jsfiddle.net/melchizidech/UGWe6/
Date.prototype.daysSince = function(newDate){
var difference = this.valueOf() - newDate.valueOf();
var msInDay = 1000 * 60 * 60 * 24;
var days = Math.floor(difference / msInDay);
return days;
};

JavaScript: check if date A is at most 3 times earlier/late than date B

I have to write a JavaScript function that checks if two dates (formatted dd/MM/yyyy) make a time interval of at most 3 months.
I can retrieve the two values from two textboxes (no need to check formatting, I have been given a calendar control that automatically formats the date correctly).
I have almost no experience with JavaScript. Can you help me?
Examples:
15/2/2011, 13/2/2011 -> return true
6/1/2011, 5/10/2010 -> return false
I already check that date A is later than date B (the calendar does it for me)
No need for a ton of code:
function days_between(date1, date2) {
return Math.round(Math.abs(date1 - date2) / (1000 * 60 * 60 * 24)) > 90;
}
date1 and date2 are Date objects e.g.
var date1 = new Date('mm/dd/yyyy');
You can find difference between two dates and return value accordingly.
function days_between(date1, date2) {
// The number of milliseconds in one day
var ONE_DAY = 1000 * 60 * 60 * 24
// Convert both dates to milliseconds
var date1_ms = date1.getTime()
var date2_ms = date2.getTime()
// Calculate the difference in milliseconds
var difference_ms = Math.abs(date1_ms - date2_ms)
// check converting back to days and return
return (Math.round(difference_ms/ONE_DAY) >90);
}
If you are unable to check or parse date correctly then you should use
var x=txtDate1.split("/"); //Here txtDate1 and txtDate2 are values from your textbox
var y=txtDate2.split("/");
//date format(Fullyear,month,date)
var date1=new Date(x[2],(x[1]-1),x[0]);
var date2=new Date(y[2],(y[1]-1),y[0])

Categories

Resources