Convert date string into hours:mins that have passed [duplicate] - javascript

This question already has answers here:
JavaScript - Get minutes between two dates
(12 answers)
Difference between dates, rounded result to nearest minute
(3 answers)
Closed 2 years ago.
I have the following date string: 2020-04-21T15:28:26.000Z
I would like to convert it into the amount of hours and minutes that have passed since that.
For example: 5:10

function getPassedTime(dateStr){
const date = new Date(dateStr);
const now = new Date();
var diff = now.getTime() - date.getTime();
var diffInMinutes = Math.round(diff / 60000);
var hours = (diffInMinutes / 60);
var passeHours = Math.floor(hours);
var minutes = (hours - passeHours) * 60;
var passedMinutes = Math.round(minutes);
return passeHours+":"+passedMinutes;
}
console.log(getPassedTime('2020-04-21T15:28:26.000Z'))

function getDiffTime(time) {
const old = new Date(time)
const now = new Date();
const diff = now - old;
const msInHrs = 1000 * 60 * 60;
const msInMn = 1000 * 60;
const hrs = Math.floor(diff / msInHrs);
const mn = Math.floor((diff % (hrs * msInHrs)) / msInMn);
return `${hrs}:${mn}`;
}
console.log(getDiffTime('2020-04-21T15:28:26.000Z'));

Related

Where am I going wrong with my math here to try and extract the correct hours from a date time?

I have a countdown timer and for some reason I can not get the hours to display correctly. No matter what, the hours is always shown as zero. Can someone double check my math to see where I am going wrong?
let countTitle = '';
let countDate = '';
let countDateValue = new Date();
const second = 1000;
const minute = second * 60;
const hour = minute * 60;
const day = hour * 24;
const today = new Date().toISOString().split("T")[0];
function countdown() {
const todayValue = new Date().getTime();
const difference = countDateValue - todayValue;
const days = Math.floor(difference / day);
const hours = Math.floor((difference % day) / hour);
const minutes = Math.floor((difference % hour) / minute);
const seconds = Math.floor((difference % minute) / second);
}
function getData(e) {
e.preventDefault();
countTitle = e.srcElement[0].value;
countValue = e.srcElement[1].value;
countDateValue = new Date(countValue).getTime();
countdown();
}
I don't see any issue with your code, it gives me perfect results unless input dates itself is wrong.
When I ran your code thorough some sample dates(1619628379441, 1619227379441), it results me perfectly 4 15 23 20.
let countTitle = '';
let countDate = '';
let countDateValue = new Date();
const second = 1000;
const minute = second * 60;
const hour = minute * 60;
const day = hour * 24;
const today = new Date().toISOString().split("T")[0];
function countdown() {
const todayValue = 1619227379441; //new Date().getTime();
const difference = countDateValue - todayValue;
const days = Math.floor(difference / day);
const hours = Math.floor((difference % day) / hour);
const minutes = Math.floor((difference % hour) / minute);
const seconds = Math.floor((difference % minute) / second);
console.log(days, hours, minutes, seconds)
}
function getData(e) {
countDateValue = 1619628379441; // new Date(countValue).getTime();
countdown();
}
getData();

How can I calculate the difference between two times that are in 24 hour format which are in different dates?

In JavaScript, how can I calculate the difference between two times that are in 24 hour format which are having different dates?
Example:
Date1 is 2019/12/31 11:00:06 AM
Date2 is 2020/01/01 01:10:07 PM.
Time difference should be 02:10:13 in hh:MM:ss format
..how can get like this when date changes in appscript
Just use the Date
const dateDiffMs = (date1,date2 ) => {
const d1 = new Date(date1);
const d2 = new Date(date2);
return d1.getTime() - d2.getTime()
}
const ms2hms = (ms) => {
const sec = Math.floor(ms / 1000)
const min = Math.floor(sec / 60)
const h = Math.floor(min / 60)
return [
h,
min % 60,
sec % 60,
];
};
const format = (n) => n < 10 ? '0' + n : n;
const hms2str = ([h, min, sec]) => {
return `${h}:${format(min)}:${format(sec)}`
}
alert(hms2str(ms2hms(dateDiffMs('2020/01/01 01:10:07 PM', '2019/12/31 11:00:06 AM')))); // 26:10:01
This code works correctly if both date1 and date2 are in the same timezone. But i would recommend you to use moment.js or some other library
I would do this by gathering the date in second since whenever computers decided to keep track of time for us sometime in the 70's (epoch). Then pass it the second value and subtract, leaving the difference.
You would then need to convert it back to a date format I presume:
(function(){
var dateOneSeconds = new Date().getTime() / 1000;
setTimeout(function(){
var dateTwoSeconds = new Date().getTime() / 1000;
var seconds = dateTwoSeconds - dateOneSeconds;
console.log(seconds);
var timeDifferenceInDate = new Date(seconds * 1000).toISOString().substr(11, 8);
console.log(timeDifferenceInDate);
}, 3000);
})();
NOTE: I have used a timeout function - you will already have two dates that do not match to pop in.
EDIT: having been notified the days will not be calculated, you could maybe use date to calculate your time in seconds then use Math to create your display:
(function(){
var dateOneSeconds = new Date().getTime() / 1000;
setTimeout(function(){
var dateTwoSeconds = new Date().getTime() / 1000;
var seconds = dateTwoSeconds - dateOneSeconds;
console.log(seconds);
/* var timeDifferenceInDate = new Date(seconds * 1000).toISOString().substr(11, 8); */
seconds = Number(seconds);
var d = Math.floor(seconds / (3600*24));
var h = Math.floor(seconds % (3600*24) / 3600);
var m = Math.floor(seconds % 3600 / 60);
var s = Math.floor(seconds % 60);
timeDifferenceInDate = d + ':' + h + ':' + m + ':' + s;
console.log(timeDifferenceInDate);
}, 3000);
})();

How can I get the amount of months and years between two dates in Javascript? [duplicate]

This question already has answers here:
Difference between two dates in years, months, days in JavaScript
(34 answers)
Closed 3 years ago.
I want to get the number of years and months using Javascript, but I am not able to get to get them:
var date=new Date("2018-09-02")
document.body.innerHTML=calculateAge(date) //should print 1.1 year(s)
function calculateAge(date) {
var ageDifMs = Date.now() - date;
var ageDate = new Date(ageDifMs);
return Math.abs(ageDate.getUTCFullYear() - 1970);
}
View JSFiddle
I have researched a lot, but I wasn't able to find the right approach to print the difference in yy.mm format which is indicating year and months.
You should check the conversions first before asking... Here
function toYear(dateOne, dateTwo){
var milToYear = 1000 * 60 * 60 * 24 * 365 // 1000 to 1 sec * 60 for 60 sec * 60 for min * 24 for hours 365
var difDate = dateOne.getTime() - dateTwo.getTime();
var result = difDate / milToYear;
console.log(result);
return result;
}
var date = new Date();
var date2 = new Date('2018-09-02');
toYear(date, date2);
var date = new Date("2018-09-02");
var age = calculateAge(date);
document.body.innerHTML = age;
function calculateAge(date) {
var dateNow = Date.now();
// To calculate the time difference of two dates
var Difference_In_Time = dateNow - date.getTime();
console.log("Difference_In_Time : " + Difference_In_Time);
// To calculate the no. of days between two dates
var Difference_In_Days = Difference_In_Time / (1000 * 3600 * 24);
console.log("Difference_In_Days: " + Difference_In_Days);
// To calculate difference in Years
var Difference_In_Years = Difference_In_Days / 365
console.log("Difference_In_Years: " + Difference_In_Years);
return Difference_In_Years;
}

How to set the date to be 3 minutes ahead [duplicate]

This question already has answers here:
How to add 30 minutes to a JavaScript Date object?
(29 answers)
Closed 3 years ago.
I need a timer for my program so my idea was to use the Date() and a future date to make the difference between them a timer,but i have been having problems with Date() functions,where the future returns null
var date = new Date();
var future = date.setMinutes(date.getMinutes + 3);
console.log(future); //prints NAN
var distance = future - date;
var minutes = Math.floor((distance % (1000 * 60 * 60)) / (1000 * 60));
var seconds = Math.floor((distance % (1000 * 60)) / 1000);
use getMinutes as a method call and add 3 to that.
let future = new Date((date = new Date()).setMinutes(date.getMinutes() + 3));
let future = new Date((date = new Date()).setMinutes(date.getMinutes() + 3));
console.log(future.toLocaleString());
use getMinutes as a method call, not a property, and add 3 to that.
let future = new Date((date = new Date()).setMinutes(date.getMinutes() + 3));
let future = new Date((date = new Date()).setMinutes(date.getMinutes() + 3));
console.log(future);

Return minutes from date difference [duplicate]

This question already has answers here:
JavaScript - Get minutes between two dates
(12 answers)
Closed 7 years ago.
I have this function:
dateDifference: function(start_date, end_date)
{
var date1 = new Date(start_date);
var date2 = new Date(end_date);
var timeDiff = Math.abs(date2.getTime() - date1.getTime());
return timeDiff;
}
how you can see I calculate the difference between two dates passed as parameter, now the end result is like this:
55000
But I want the result in minutes how I can achieve this?
You got milliseconds so you can divide them by 1000 and 60 and get result in minutes.
dateDifference: function(start_date, end_date)
{
var date1 = new Date(start_date);
var date2 = new Date(end_date);
var timeDiff = Math.abs((date2.getTime() - date1.getTime()) / 1000 / 60);
return timeDiff;
}
to get from 55000 to seconds, divide by 1000.
then divide by 60 to get minutes.
like so:-
function dateDifference(start_date, end_date)
{
var date1 = new Date(start_date);
var date2 = new Date(end_date);
var milSeconds = Math.abs(date2.getTime() - date1.getTime());
var seconds = milSeconds / 1000;
var minutes = seconds / 60;
return minutes;
}
console.log(dateDifference('01/12/2016 09:00:00', '01/12/2016 10:00:00')); // 60 minutes

Categories

Resources