Click here to Skip to main content
15,887,936 members

Welcome to the Lounge

   

For discussing anything related to a software developer's life but is not for programming questions. Got a programming question?

The Lounge is rated Safe For Work. If you're about to post something inappropriate for a shared office environment, then don't post it. No ads, no abuse, and no programming questions. Trolling, (political, climate, religious or whatever) will result in your account being removed.

 
GeneralRe: Today Pin
Sander Rossel11-Nov-20 3:33
professionalSander Rossel11-Nov-20 3:33 
GeneralMessage Closed Pin
11-Nov-20 4:16
mvahoney the codewitch11-Nov-20 4:16 
GeneralRe: Today Pin
W Balboos, GHB11-Nov-20 4:24
W Balboos, GHB11-Nov-20 4:24 
GeneralRe: Today Pin
W Balboos, GHB11-Nov-20 4:21
W Balboos, GHB11-Nov-20 4:21 
GeneralRe: Today Pin
DRHuff11-Nov-20 5:49
DRHuff11-Nov-20 5:49 
GeneralRe: Today Pin
dandy7211-Nov-20 10:43
dandy7211-Nov-20 10:43 
GeneralRe: Today Pin
fd975011-Nov-20 6:06
professionalfd975011-Nov-20 6:06 
RantJavaScript, not even once PinPopular
Sander Rossel10-Nov-20 22:27
professionalSander Rossel10-Nov-20 22:27 
So I have this super simple use case.
I have a starting time and an ending time and I want to calculate the total time between them (just hours and minutes).
I use three time inputs in HTML and set up an event handler on the change event of the start en end times.
My initial thought was something like total = end.value - start.value, but unfortunately, the value is a string.
Search around and I can do Date.parse(start.value) to get an actual Date object... But wait, no, it returns an integer representing the milliseconds since 1970...
Now, new Date(Date.parse(start.value)) gives me a proper Date object.
But, the control gives me the date of 1970 since the date doesn't matter because it's a time only input.
However, the other control probably still has the date set from the (C#) back-end.
So, get Date.now, which also returns milliseconds, so new Date(Date.now), and set both dates to today so I can calculate the difference.
And now it all goes to hell.
The input assumed UTC, I think, while the Date object somehow corrects for my time zone (UTC + 1).
Or maybe the Date is neutral, but the eventual toISOString isn't (same result with toString though).
Whatever it is, it only does this for the value I just edited, so my result is off by an hour.
Tried all kinds of stuff, always off by an hour.
Even when I manually correct for the difference, there's ultimately a difference.
Ended up reading the string from the input, manually substringing the hours and minutes, build two new dates using nothing but setUTC methods and then subtracting those.
That works and I can easily correct for times like 23:00 (start) and 01:00 (end) by adding a day to end when end > start (the difference well never be larger than a couple of hours, certainly not 24 hours).

HOW Elephant | [mastadon] ING HARD CAN IT BE!? Mad | :mad:
This kind of sh*t is exactly why I hate (front-end) web development with the burning passion of a thousand suns.
And don't even get me started on CSS Dead | X|

The reason I'm so pissed is because it took me until 01:30 to fix this, went to bed all worked up and didn't sleep until 04:30 Dead | X|
Gave me a sore throat to boot Cry | :((
Really, Elephant | [mastadon] JavaScript Mad | :mad: Mad | :mad: Mad | :mad: Mad | :mad: Mad | :mad: Mad | :mad:

GeneralRe: JavaScript, not even once Pin
Richard Deeming10-Nov-20 22:48
mveRichard Deeming10-Nov-20 22:48 
GeneralRe: JavaScript, not even once Pin
Sander Rossel10-Nov-20 23:27
professionalSander Rossel10-Nov-20 23:27 
GeneralRe: JavaScript, not even once Pin
Kornfeld Eliyahu Peter10-Nov-20 23:45
professionalKornfeld Eliyahu Peter10-Nov-20 23:45 
GeneralRe: JavaScript, not even once Pin
Sander Rossel10-Nov-20 23:50
professionalSander Rossel10-Nov-20 23:50 
GeneralRe: JavaScript, not even once Pin
Kornfeld Eliyahu Peter11-Nov-20 0:00
professionalKornfeld Eliyahu Peter11-Nov-20 0:00 
GeneralRe: JavaScript, not even once Pin
Richard Deeming11-Nov-20 0:42
mveRichard Deeming11-Nov-20 0:42 
GeneralRe: JavaScript, not even once Pin
Kornfeld Eliyahu Peter11-Nov-20 1:04
professionalKornfeld Eliyahu Peter11-Nov-20 1:04 
GeneralRe: JavaScript, not even once Pin
Sander Rossel11-Nov-20 4:16
professionalSander Rossel11-Nov-20 4:16 
GeneralRe: JavaScript, not even once Pin
theoldfool11-Nov-20 1:40
professionaltheoldfool11-Nov-20 1:40 
GeneralRe: JavaScript, not even once Pin
BillWoodruff11-Nov-20 2:08
professionalBillWoodruff11-Nov-20 2:08 
GeneralRe: JavaScript, not even once Pin
raddevus11-Nov-20 2:41
mvaraddevus11-Nov-20 2:41 
GeneralRe: JavaScript, not even once Pin
Sander Rossel11-Nov-20 3:27
professionalSander Rossel11-Nov-20 3:27 
GeneralRe: JavaScript, not even once Pin
raddevus11-Nov-20 3:44
mvaraddevus11-Nov-20 3:44 
GeneralRe: JavaScript, not even once Pin
honey the codewitch11-Nov-20 3:59
mvahoney the codewitch11-Nov-20 3:59 
GeneralRe: JavaScript, not even once Pin
Sander Rossel11-Nov-20 4:05
professionalSander Rossel11-Nov-20 4:05 
GeneralRe: JavaScript, not even once Pin
honey the codewitch11-Nov-20 4:08
mvahoney the codewitch11-Nov-20 4:08 
GeneralRe: JavaScript, not even once Pin
Marc Clifton11-Nov-20 6:52
mvaMarc Clifton11-Nov-20 6:52 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.