Subtracting time from current time [duplicate] - javascript

This question already has answers here:
How can I subtract hours from a HH:MM AM time string in Javascript?
(7 answers)
Closed 1 year ago.
I have 2 strings, that have the minute and hour that a function needs to occur at.
I want to check, if the minute and hour which are specified in string format, and from my database are within 5 minutes of the current time, call this function.
My original thought was something like:
(today.minute is the current minute)
today.minute:
minute = "55"
hour = "14"
var today = new Date();
var time = today.getMinutes(), today.getHours()
if (today.getMinutes() - 5 == minute) {
myFunc()
}
But that isn't going to work, because I need the hour and minute - 5 minutes... how can I do this?

Is this what you are looking for?
let minute = "55"
let hour = "14"
let today = new Date();
//var time = today.getMinutes(), today.getHours()
if (Math.abs(today.getMinutes() - Number(minute)) <= 5) {
myFunc()
}
function myFunc() {
console.log('myFunc called', today.getMinutes());
}

Related

Getting only hours and minutes from date object [duplicate]

This question already has answers here:
How do I use .toLocaleTimeString() without displaying seconds?
(13 answers)
How do you display JavaScript datetime in 12 hour AM/PM format?
(31 answers)
Closed 3 years ago.
I want to get a JavaScript code that displays only hour, minute and AM or PM. I don't want seconds.
This is the code i have so far:
var d = new Date();
document.getElementById("time").innerHTML = d.toLocaleTimeString();
Date objects have getHours() and getMinutes() methods.
getHours() returns values in the range of [0, 23], meaning you can compare with 12 to determine 'am' or 'pm'.
Lastly, consider using mdn as a helpful reference about the standard API.
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/getHours
let d = new Date('December 17, 1995 22:11:51');
let pm = d.getHours() >= 12;
let hour12 = d.getHours() % 12;
if (!hour12)
hour12 += 12;
let minute = d.getMinutes();
console.log(`${hour12}:${minute} ${pm ? 'pm' : 'am'}`);

How to calculate is more than one second/hour passed using milliseconds? [duplicate]

This question already has answers here:
Getting difference in seconds from two dates in JavaScript
(2 answers)
Closed 3 years ago.
I want to calculate the time difference between two dates which have different format to know if more than one hour/seconds has passed.
The first format is: 2019-07-02T16:21:00.1030000
The second one is: 1562160899773
I am using getMilliseconds() but I don't know if it's the better way.
for example
var isMoreOneSec = New Date(2019-07-02T16:21:00.1030000).getMilliseconds() - new Date(1562160899773).getMilliseconds() > 1000
getMilliseconds() will return the number of milliseconds in the current second, hence will always return from 0 to 999, the same way getHour() will return from 0 to 23 or getSeconds() from 0 to 59.
When you have two dates, to get the difference in milliseconds you have to use getTime() which will return the number of milliseconds sice unix epoch.
So:
var date1 = new Date("2019-07-02T16:21:00.1030000");
var date2 = new Date(1562160899773);
var date1time = date1.getTime();
var date2time = date2.getTime();
console.log("Date 1 time is", date1time);
console.log("Date 2 time is", date2time);
var dif = date2time - date1time;
console.log("Dif in ms is ", dif);
console.log("Is dif more than one second?", dif > 1000);
console.log("Is dif more than one hour?", dif > 60*60*1000);

How do i check given duration times (00:20:40,1:20:40,00:00:10) is <20sec, >1hour and <10 seconds [duplicate]

This question already has answers here:
How can I convert a HH:mm:ss string to a JavaScript Date object?
(3 answers)
Closed 4 years ago.
I am having a field called Duration it contains time like 00:20:40.How do i check given duration times (00:20:40,1:20:40,00:00:10) is <20sec, >1hour and <10 seconds .I tried the following but didn't work.
var time = new Date('00:10:40');
time.getMinutes();
Output will look like:
The given time is <20 minute.Hence i need to check like this
if(<20 minutes){...}
You have to create Date Object with Date to use it.
var d = new Date("1970-01-01 20:18:02");
document.write(d.getMinutes());
You can do the following:
var time = "00:20:40".split(":");
var minutes = time[1];
The given string "00:20:40" is not a valid date string and cannot be passed to new Date() as an argument. In this case, you can use the above solution which will split the string and give you an array consisting of [hh, mm, ss] and you will be able to get the minutes at time[1].
I hope it helps.
function toSeconds (duration) {
const regex = /(\d+):(\d+):(\d+)/;
const matched = duration.match(regex);
const hours = parseInt(matched[1]);
const minutes = parseInt(matched[2]);
const seconds = parseInt(matched[3]);
return (hours * 60 * 60) + (minutes * 60) + seconds;
}
function toMinutes (duration) {
const seconds = toSeconds(duration);
return seconds / 60;
}
function toHours (duration) {
const minutes = toMinutes(duration);
return minutes / 60;
}
toSeconds('00:20:40') // 1240
toMinutes('00:20:40') // 20.666666666666668
toMinutes('01:20:40') // 80.66666666666667
toHours('01:20:40') // 1.3444444444444446

Calculate time difference | strings [duplicate]

This question already has answers here:
JavaScript - Get minutes between two dates
(12 answers)
How can I compare two time strings in the format HH:MM:SS?
(16 answers)
Closed 6 years ago.
I want to display the amount of minutes between the scheduled time and expected time.
This is not to compare, this is to calculate how many minutes there are in different times in both scheduled and expected.
Since both times are displayed as a string, do I need to convert string to a number and then do a comparison?
All I want to return is the difference in time as a number.
Here is my object:
{
station: "Macclesfield",
scheduled: "15:41",
expected: "15:50",
platform: "1"
}
var data = {
station: "Macclesfield",
scheduled: "15:41",
expected: "15:50",
platform: "1"
}
function getTimeDifference(scheduled, expected) {
scheduled = scheduled.split(':'); //get array [hours, minutes]
expected = expected.split(':');
var hours = expected[0] - scheduled[0]; //difference in hours
var minutes = expected[1] - scheduled[1]; //difference in minutes
if (minutes < 0) { //if minutes are negative we know it wasn't a full hour so..
hours--; //subtract an hour
minutes += 60; //add 60 minutes
} //now we're ok
if (hours) //if hours has a value
return hours + ':' + minutes;
return minutes; //hours is 0 so we only need the minutes
}
console.log(getTimeDifference(data.scheduled, data.expected));
data.expected = "16:00";
console.log(getTimeDifference(data.scheduled, data.expected));
data.expected = "17:00";
console.log(getTimeDifference(data.scheduled, data.expected));
var obj = { scheduled: "15:41", expected: "15:50" }
var milliSeconds = Date.parse(`01/01/2011 ${obj.expected}:00`) - Date.parse(`01/01/2011 ${obj.scheduled}:00`)
var minutes = milliSeconds / (1000 * 60)
var hours = milliSeconds / (1000 * 60 * 60)

How to make my clock show 6 digits with javascript? [duplicate]

This question already has answers here:
Leading zeros in minutes
(6 answers)
Closed 8 years ago.
I have a clock made that displays the current time in Hours:Minutes:Seconds. Currently the time is displayed as so 20:30:25, but my problem is that when the time is a single digit for example 1.05 (3 seconds) am, it will appear like this on the clock 1:5:3. I want it to appear like this 01:05:03.
How can this be done?
var today = new Date();
var hours = today.getHours();
var minutes = today.getMinutes();
var seconds = today.getSeconds();
today = hours+':'+minutes+':'+seconds;
document.write(today);
A simple way is to use a slice(-2) trick to always let the number with a preceding zero (if needed)
Slice, with a negative parameter, takes the last n characters of a string, in this case, always a two digit value:
var today = new Date();
var hours = ("00" + today.getHours()).slice(-2);
var minutes = ("00" + today.getMinutes()).slice(-2);
var seconds = ("00" + today.getSeconds()).slice(-2);
alert(hours+":"+minutes+":"+seconds);
Simply add zeros! I used ternary operator to do it.
var today = new Date();
var hours = today.getHours() <10 ? "0"+today.getHours(): today.getHours();
var minutes = today.getMinutes() <10 ? "0"+today.getMinutes(): today.getMinutes();
var seconds = today.getSeconds() <10 ? "0"+today.getSeconds(): today.getSeconds();
today = hours+':'+minutes+':'+seconds;
document.write(today);

Categories