jquery if hours and minutes <> [duplicate] - javascript

This question already has an answer here:
The correct way to compare time in javascript? [duplicate]
(1 answer)
Closed 1 year ago.
Please, How can I set IF when time is < 21:30 ??
var dt = new Date();
if ((dt.getHours() <= 21) && (dt.getMinutes() <= 30)) { alert("check"); }
This not working when time is example 20:45

You need to check two different things.
if hours <= 20, than everything is true.
if hours == 21, than check minutes.
var dt = new Date('2021/03/18 20:45:00');
if (dt.getHours() <= 20 || (dt.getHours() == 21 && dt.getMinutes() <= 30)) {
alert("check");
}

You could always take the time and convert it to minutes in the day - 1440 (60*24) so 21:30 becomes 21 * 60 + 30 = 1,290.
We can calculate the current minute value by taking the current date time Date.now() (milliseconds since 1/1/1970) mod 86,400,000 (milliseconds in a day) * further divide this by 60,000 (milliseconds in a minute).
(Date.now() % 86_400_000) / (60_000)
It is then trivial to compare these two values
const nineFortyFivePM = 21 * 60 + 30;
const currentMinutes = (Date.now() % 86_400_000) / (60_000);
console.log(`21:45 = ${nineFortyFivePM}`);
console.log(`currentMinutes = ${currentMinutes}`);
if (currentMinutes < nineFortyFivePM)
alert('check');

Related

Snowflake UDF in JavaScript does not calculate as expected

I am trying to calculate the number of minutes a logged job has been running.
Each job has a start time and an end time.
In this particular case, the working hours are between 01:00 and 10:00, and only business days (weekends excluded)
In order to calculate this, I tried and made a JavaScript based UDF like this:
CREATE OR REPLACE FUNCTION JobRuns(f datetime, t datetime)
RETURNS DOUBLE
LANGUAGE JAVASCRIPT
AS
$$
// Based on the Calculation of Business Hours in JavaScript
// https://www.c-sharpcorner.com/UploadFile/36985e/calculating-business-hours-in-javascript/
function workingMinutesBetweenDates(startDate, endDate) {
// Store minutes worked
var minutesWorked = 0;
// Validate input
if (endDate < startDate) {
return 0;
}
// Loop from your Start to End dates (by hour)
var current = startDate;
// Define work range
var workHoursStart = 1;
var workHoursEnd = 10;
var includeWeekends = false;
// Loop while currentDate is less than end Date (by minutes)
while (current <= endDate) {
// Is the current time within a work day (and if it occurs on a weekend or not)
if (current.getHours() >= workHoursStart && current.getHours() <= workHoursEnd && (includeWeekends ? current.getDay() !== 0 && current.getDay() !== 6 : true)) {
minutesWorked++;
}
// Increment current time
current.setTime(current.getTime() + 1000 * 60);
}
// Return the number of minutes
return minutesWorked;
}
return workingMinutesBetweenDates(F,T);
$$
;
But the result I am getting are in some cases rather off from what I had expected.
The JS logic is grabbed from here; https://www.c-sharpcorner.com/UploadFile/36985e/calculating-business-hours-in-javascript/ and when I look at the code, I cannot see any flaws, which might cause these discrepancies.
I am using these test data
CREATE OR REPLACE TABLE "SLA_Test" (
"DocumentID" VARCHAR(16777216),
"From" TIMESTAMP_NTZ(9),
"To" TIMESTAMP_NTZ(9),
"ExpectedTime" INT
);
INSERT INTO "SLA_Test"
VALUES
('ACD7EFC1-8D17-46E3-84DB-C08067466866','2021-03-03 07:12:34.567','2021-03-03 08:12:34.567',60),
('C41FB599-D1EC-4461-BBAF-1AFF67D2F3C2','2021-03-03 09:55:00.000','2021-03-04 01:05:00.000',10),
('B741C663-732B-4FD3-839D-E70330C58990','2021-03-03 09:55:00.000','2021-03-04 00:05:00.000',5),
('C5893C51-F5CE-40E4-85F7-775515BC3E3D','2021-03-03 19:55:00.000','2021-03-04 01:05:00.000',5),
('BAF4ED57-8184-4CDF-8875-DFDA6EAC2033','2021-03-03 09:55:00.000','2021-03-05 01:05:00.000',550),
('F325059E-E78F-4DCE-B675-CC1C59669B3C','2021-03-05 09:55:00.000','2021-03-08 01:05:00.000',10),
('F325059E-E78F-4DCE-B675-CC1C59669B3C','2021-03-05 09:55:00.000','2021-03-07 01:05:00.000',5);
SELECT "DocumentID","From","To",
DATEDIFF(second, "From", "To") AS "TotalElapsedTimeSecond",
DATEDIFF(second, "From", "To")/60 AS "TotalElapsedTimeMinut",
"ExpectedTime",
JobRuns("From","To") AS "ElapsedTimeMinut"
FROM "SLA_Test";
Any ideas why the UDF does not return the expected time?
If you create a working hours table, you can run the following query:
select
t.id
, sum(datediff(‘second’,
-- calculate the max of the two start time
(case when t.start <=
w.working_day_start_timestamp
then w.working_day_start_timestamp
else t.start
end),
-- calculate the min of the two end times
(case when t.end >=
w.working_day_end_timestamp
then w.working_day_end_timestamp
else t.end
end)
)) / 3600 -- convert to hourly
as working_hour_diff
from
working_days_times w,
cross join time_intervals t
where -- select all intersecting intervals
(
t.start <= w.working_day_end_timestamp
and
t.end >= w.working_day_start_timestamp
)
and -- select only working days
w.is_working_day
group by
t.id
This article also goes into more detail about implementing this as a Javascript UDF: https://medium.com/dandy-engineering-blog/how-to-calculate-the-number-of-working-hours-between-two-timestamps-in-sql-b5696de66e51
this can all be done in SQL,
with SLA_Test(DocumentID, FromTime, ToTime, ExpectedTime) AS (
SELECT column1, column2::timestamp_ntz, column3::timestamp_ntz, column4
FROM
VALUES
('ACD7EFC1-8D17-46E3-84DB-C08067466866','2021-03-03 07:12:34.567','2021-03-03 08:12:34.567',60),
('C41FB599-D1EC-4461-BBAF-1AFF67D2F3C2','2021-03-03 09:55:00.000','2021-03-04 01:05:00.000',10),
('B741C663-732B-4FD3-839D-E70330C58990','2021-03-03 09:55:00.000','2021-03-04 00:05:00.000',5),
('C5893C51-F5CE-40E4-85F7-775515BC3E3D','2021-03-03 19:55:00.000','2021-03-04 01:05:00.000',5),
('BAF4ED57-8184-4CDF-8875-DFDA6EAC2033','2021-03-03 09:55:00.000','2021-03-05 01:05:00.000',550),
('F325059E-E78F-4DCE-B675-CC1C59669B3C','2021-03-05 09:55:00.000','2021-03-08 01:05:00.000',10),
('F325059E-E78F-4DCE-B675-CC1C59669B3C','2021-03-05 09:55:00.000','2021-03-07 01:05:00.000',5)
), days as (
SELECT row_number() over(order by seq8())-1 as num
FROM table(GENERATOR(rowcount=>30))
), enriched as (
SELECT *,
datediff('day', s.fromtime, s.totime) as tot_days
from SLA_Test AS s
), day_sliced AS (
select s.*
,d.*
,date_trunc('day',fromtime) f_s
,dateadd('day', d.num, f_s) as clip_day
,dateadd('hour', 1, clip_day) as clip_start
,dateadd('hour', 10, clip_day) as clip_end
,dayofweekiso(clip_day) as dowi
,dowi >=1 AND dowi <= 5 as work_day
,least(greatest(s.fromtime, clip_start),clip_end) as slice_start
,greatest(least(s.totime, clip_end), clip_start) as slice_end
,DATEDIFF('second', slice_start, slice_end) as slice_sec
,DATEDIFF('minute', slice_start, slice_end) as slice_min
from enriched AS s
join days AS d on d.num <= s.tot_days
qualify work_day = true
)
SELECT
DocumentID
,FromTime
,ToTime
,ExpectedTime
,round(sum(slice_sec)/60,0) as elasped_time_minutes
FROM day_sliced
GROUP BY 1,2,3,4
ORDER BY 1,2;
which gives the results as noted in expected:
DOCUMENTID FROMTIME TOTIME EXPECTEDTIME ELASPED_TIME_MINUTES
ACD7EFC1-8D17-46E3-84DB-C08067466866 2021-03-03 07:12:34.567 2021-03-03 08:12:34.567 60 60
B741C663-732B-4FD3-839D-E70330C58990 2021-03-03 09:55:00.000 2021-03-04 00:05:00.000 5 5
BAF4ED57-8184-4CDF-8875-DFDA6EAC2033 2021-03-03 09:55:00.000 2021-03-05 01:05:00.000 550 550
C41FB599-D1EC-4461-BBAF-1AFF67D2F3C2 2021-03-03 09:55:00.000 2021-03-04 01:05:00.000 10 10
C5893C51-F5CE-40E4-85F7-775515BC3E3D 2021-03-03 19:55:00.000 2021-03-04 01:05:00.000 5 5
F325059E-E78F-4DCE-B675-CC1C59669B3C 2021-03-05 09:55:00.000 2021-03-07 01:05:00.000 5 5
F325059E-E78F-4DCE-B675-CC1C59669B3C 2021-03-05 09:55:00.000 2021-03-08 01:05:00.000 10 10
Did you test this outside of Snowflake? I just created the following file and running node /tmp/dates.js produces this output which matches up with Snowflake
// Col1: function return, Col2: Expected
61 60
71 10
65 5
6 5
671 550
1271 10
671 5
function workingMinutesBetweenDates(startDate, endDate) {
// Store minutes worked
var minutesWorked = 0;
// Validate input
if (endDate < startDate) {
return 0;
}
// Loop from your Start to End dates (by hour)
var current = startDate;
// Define work range
var workHoursStart = 1;
var workHoursEnd = 10;
var includeWeekends = false;
// Loop while currentDate is less than end Date (by minutes)
while (current <= endDate) {
// Is the current time within a work day (and if it occurs on a weekend or not)
if (current.getHours() >= workHoursStart && current.getHours() <= workHoursEnd && (includeWeekends ? current.getDay() !== 0 && current.getDay() !== 6 : true)) {
minutesWorked++;
}
// Increment current time
current.setTime(current.getTime() + 1000 * 60);
}
// Return the number of minutes
return minutesWorked;
}
console.log(workingMinutesBetweenDates((new Date(Date.parse('2021-03-03 07:12:34.567'))), (new Date(Date.parse('2021-03-03 08:12:34.567')))), 60);
console.log(workingMinutesBetweenDates((new Date(Date.parse('2021-03-03 09:55:00.000'))), (new Date(Date.parse('2021-03-04 01:05:00.000')))), 10);
console.log(workingMinutesBetweenDates((new Date(Date.parse('2021-03-03 09:55:00.000'))), (new Date(Date.parse('2021-03-04 00:05:00.000')))), 5);
console.log(workingMinutesBetweenDates((new Date(Date.parse('2021-03-03 19:55:00.000'))), (new Date(Date.parse('2021-03-04 01:05:00.000')))), 5);
console.log(workingMinutesBetweenDates((new Date(Date.parse('2021-03-03 09:55:00.000'))), (new Date(Date.parse('2021-03-05 01:05:00.000')))), 550);
console.log(workingMinutesBetweenDates((new Date(Date.parse('2021-03-05 09:55:00.000'))), (new Date(Date.parse('2021-03-08 01:05:00.000')))), 10);
console.log(workingMinutesBetweenDates((new Date(Date.parse('2021-03-05 09:55:00.000'))), (new Date(Date.parse('2021-03-07 01:05:00.000')))), 5);
I've found at least 2 issues with the code, I think:
It will always over-count by at least 1. On the first loop round the WHILE statement the minutesWorked is incremented but at that point no time has actually been worked - the first minute hasn't been worked until StartDate + 1 minute
Your working day ends at 10 but your logic includes any time where the hour portion <= 10, so it is incrementing minutesWorked up to 10:59:59. I think the logic should be just less than not less than or equals: ... && current.getHours() < workHoursEnd

How to format minutes to hours - using moment.js [duplicate]

This question already has answers here:
how to convert minutes to hours using moment.js
(2 answers)
Closed 3 years ago.
I am using moment.js.
I get minutes (max 1440) and i wanted to format that in hours in specific format.
Something like this:
420 minutes is: 07:00
1140 minutes is: 24:00
451 minutes is: 07:31
const time = 1140;
console.log(getTime(time));
function getTime(minutes) {
let hours = parseInt(minutes / 60);
hours = hours < 10 ? `0${hours}` : hours;
let min = minutes % 60;
min = min < 10 ? `0${min}` : min;
return `${hours}:${min}`;
}

Converting UTC to Decimal time

Background
I want to create a new date/time system based on an old French version with some modifications.
This involves converting UTC date/times to new quantities:
12 months => 10 months
52 weeks => 36.5 weeks
28/31 days per month => 36/37 days per month
24 hours => 20 hours
60 minutes => 100 minutes
60 seconds => 100 seconds
I've coded a clock in JavaScript as proof of concept, but unsure as to whether I have correctly calculated everything, additionally whether it's the best approach:
Code
1) getDecimalDate() calculates the day of the year, then works out which month it exists within a new calendar of 36 or 37 days per month. Then calculates the new date of the month.
function getDecimalDate(date) {
var oldDay = 1000 * 60 * 60 * 24,
startYear = new Date(Date.UTC(date.getUTCFullYear(), 0, 0)),
day = Math.floor((date - startYear) / oldDay),
num = 0,
month = 1;
if (day > 36) { num += 36; month = 2; }
if (day > 73) { num += 37; month = 3; }
if (day > 109) { num += 36; month = 4; }
if (day > 146) { num += 37; month = 5; }
if (day > 182) { num += 36; month = 6; }
if (day > 219) { num += 37; month = 7; }
if (day > 255) { num += 36; month = 8; }
if (day > 292) { num += 37; month = 9; }
if (day > 328) { num += 36; month = 10; }
return { day: day - num, month: month, year: date.getUTCFullYear(), num: num };
}
2) getDecimalTime() calculates the number of milliseconds since midnight, then changes it from old milliseconds per day to new totals, then calculates hours, mins etc
function getDecimalTime(date) {
var oldDay = 1000 * 60 * 60 * 24,
newDay = 1000 * 100 * 100 * 20,
startDay = new Date(Date.UTC(date.getUTCFullYear(), date.getUTCMonth(), date.getUTCDate())),
delta = ((date - startDay) / oldDay) * newDay;
var hours = Math.floor(delta / 10000000) % 20;
delta -= hours * 10000000;
var minutes = Math.floor(delta / 100000) % 100;
delta -= minutes * 100000;
var seconds = Math.floor(delta / 1000) % 100;
delta -= seconds * 1000;
var milliseconds = Math.floor(delta) % 1000;
return { milliseconds: milliseconds, seconds: seconds, minutes: minutes, hours: hours };
}
You can see a working version here:
https://jsfiddle.net/kmturley/7mrwc3x3/9/
Results
Bear in mind i've made up day/month names using Latin (Nov = 9, die = day, dec = 10, mense = month)
String - Saturday December 3 => Novdie Decmense 10
Date - 03-12-2016 => 10-10-2016
Time - 22:47:52 => 18:98:43
Questions
Is the math correct?
Are there any issues with timezones? i've
tried converting all Date objects to UTC but JavaScript can be
tricky
Can I improve the code? the month selection seems like it
could be improved but I couldn't figure out a better way to count 36
and 37 day months. if (num % 36.5 === 1) wouldn't work?
Thanks!
Update - 7th December 2016 - new versions based on solution:
https://jsfiddle.net/kmturley/7mrwc3x3/10/
https://github.com/kmturley/decimal-time
Is the math correct?
You didn't say which months have 35 days and which have 36 so we have to accept that the if statements are correct. You don't show how date is created so it may or may not be OK. And you don't say what happens for leap years, this system seems to only have 365 days per year.
The following:
24 hours => 20 hours
60 minutes => 100 minutes
60 seconds => 100 seconds
doesn't seem correct. Do you actually mean:
1 day = 20 decimal hours
1 decimal hour = 100 decimal minutes
1 decimal minute = 100 decimal seconds
1 decimal second = 1000 decimal milliseconds
Your strategy of getting the time in ms and scaling to decimal ms seems fine, I'll just make the following comments.
In getDecimalTime it is simpler to calculate startDay by first copying date then setting its UTC hours to zero:
startDay = new Date(+date);
startDate.setUTCHours(0,0,0,0);
Then scale:
var diffMilliseconds = date - startDate;
var decimalMilliseconds = diffMilliseconds / 8.64e7 * 2.0e8;
so 1 standard millisecond = 2.314814814814815 decimal milliseconds
In the date function, the expression:
new Date(date.getUTCFullYear(), 0, 0)
will create a date for 31 December the previous year (i.e. date of 0), if you're after 1 January then it should be:
new Date(date.getUTCFullYear(), 0, 1);
So likely you're one day out. Otherwise, the code seems to be correct. For me, the get decimal time function would be simpler as:
function getDecimalTime(date) {
// Pad numbers < 10
function z(n){return (n<10?'0':'')+n;}
// Copy date so don't modify original
var dayStart = new Date(+date);
var diffMs = date - dayStart.setUTCHours(0,0,0,0);
// Scale to decimal milliseconds
var decMs = Math.round(diffMs / 8.64e7 * 2.0e8);
// Get decimal hours, etc.
var decHr = decMs / 1.0e7 | 0;
var decMin = decMs % 1.0e7 / 1.0e5 | 0;
var decSec = decMs % 1.0e5 / 1.0e3 | 0;
decMs = decMs % 1.0e3;
return z(decHr) + ':' + z(decMin) + ':' + z(decSec) + '.' + ('0' + z(decMs)).slice(-3);
}
// Helper to format the time part of date
// as UTC hh:mm:ss.sss
function formatUTCTime(date) {
function z(n){return (n<10?'0':'')+n;}
return z(date.getUTCHours()) + ':' +
z(date.getUTCMinutes()) + ':' +
z(date.getUTCSeconds()) + '.' +
('00' + date.getUTCMilliseconds()).slice(-3);
}
// Test 00:00:00.003 => 00:00:00.007
// i.e. 3ms * 2.31decms => 6.93decms
var d = new Date(Date.UTC(2016,0,1,0,0,0,3));
console.log(getDecimalTime(d));
// Test 12:00:00.000 => 10:00:00.000
// i.e. noon to decimal noon
var d = new Date(Date.UTC(2016,0,1,12,0,0,0));
console.log(getDecimalTime(d));
// Test current time
d = new Date();
console.log(formatUTCTime(d));
console.log(getDecimalTime(d));

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)

calculate differences between two time in minutes [duplicate]

This question already has answers here:
calculate time difference
(4 answers)
Closed 9 years ago.
How to calculate differences between two time eg(server time=04:30 pm and <p id="orderTime">02:30 pm</p>) and return it in minutes such as 90 minutes using jquery and javascript. The server time and order time is in 12 hour format not in 24h.
If you can guarantee they always will be of that format, then here is a straightforward solution:
function humanReadableToMinutes(time)
{
var parts = time.split(/ |:/);
return (parts[2] == 'pm' * 12 * 60)
+ parseInt(parts[0], 10) * 60
+ parseInt(parts[1], 10);
}
http://jsfiddle.net/aYwux/2/
Explanation:
By var parts = time.split(/ |:/); we're splitting 02:30 pm into 3 parts: 02, 30, pm.
return (parts[2] == 'pm' * 12 * 60) + parseInt(parts[0], 10) * 60 + parseInt(parts[1], 10); contains of 3 operands:
* (parts[2] == 'pm' * 12 * 60) --- adds additional 12 hours if it's "pm"
* parseInt(parts[0], 10) * 60 - takes hours fraction extracted and converts it to minutes
* parseInt(parts[1], 10) - minutes fraction
PS: this solution doesn't take care of 12am and 12pm accurately, so it's a homework for the OP

Categories