|
Yeah, I was thinking about Moment.js, but I know it's no longer supported.
Too bad too, I really needed that Klingon format
I didn't know Day.js and Luxon, but they look promising.
I didn't want to use a library for something this simple.
I dislike using libraries for a single line in my entire application (unless it's really something complex).
Then when I found out it wasn't that simple it got personal and I wanted to fix it myself.
At least it works now and I'm the wiser for it
|
|
|
|
|
Not that I can explain it for your case, but it seems the two dates (times) are in different timezone...
I work a lot with dates and times in JS and functions that handle dates and times are part of my base library...
What I've seen is that JS will try to be super smart and change the timezone according to your date, even it is totally BS...
For instance, working with local - Israeli - time - times with 01/01/1970 as date part will add 2:30, while times with today as date part will add 3 hours... Totally nuts!
"The only place where Success comes before Work is in the dictionary." Vidal Sassoon, 1928 - 2012
|
|
|
|
|
Kornfeld Eliyahu Peter wrote: times with 01/01/1970 as date part will add 2:30, while times with today as date part will add 3 hours... Totally nuts! That's probably the problem I had as well!
The date I just edited was always in 1970 (and had the off hour) while the other date was always today (or tomorrow).
Maybe time zones switched somewhere between the 70's and now?
Kornfeld Eliyahu Peter wrote: Totally nuts! JavaScript: The Definitive Guide!
|
|
|
|
|
There were (and are) changes... And not all countries have nice 1 hour jumps, it can be half or even a quarter hour...
It seems that JS try to follow the course of history but gets lost...
"The only place where Success comes before Work is in the dictionary." Vidal Sassoon, 1928 - 2012
|
|
|
|
|
Sander Rossel wrote: Maybe time zones switched somewhere between the 70's and now?
Lots of times!
And future time-zone changes are why you can't always rely on storing UTC:
Storing UTC is not a silver bullet | Jon Skeet's coding blog[^]
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
Actually storing time as UTC is wrong a lot of times...
I'm working a lot of with courses, and if a meeting in the course is at 8:00 than it is at 8:00 even if its DTS or not... It should not move to 9:00 or 7:00 when moving the clock...
"The only place where Success comes before Work is in the dictionary." Vidal Sassoon, 1928 - 2012
|
|
|
|
|
I guess you should store UTC and time zone separately and then adjust for the stored time zone every time you show the data
Although if you have a meeting at 9:00 (first thing in the morning) and you plan the meeting in UTC+2 you still want to see 9:00 when the time moves to UTC+1 if everyone is in the same time zone as you.
I recently had this issue when I was invited for a meeting in Canada in time zone x while I was in time zone UTC+2, but UTC+1 at the time of the meeting.
They should really just abandon time zones altogether.
People in England would live from 08:00 to 23:00, Europe from 09:00 to 00:00 and USA from 14:00 to 05:00 (or whatever, haven't done the math on that).
At least when you plan a meeting at 09:00 everyone will have the same time and for some it will be during day while for others it will be during the night.
On the other hand, when someone says they have to be at work at 18:00 you have no idea if that's late or early in their specific region.
Plan B, abandon the sun so everything's dark all the time and we can all live at the same time because there's no night or day anyway
|
|
|
|
|
You are doing it all wrong. Do it like today's generation.
"Hey Siri, How much time between time1 and time2? "
eezy schmeezy.
If you can keep your head while those about you are losing theirs, perhaps you don't understand the situation.
|
|
|
|
|
Bummer, dude. Take a break
«One day it will have to be officially admitted that what we have christened reality is an even greater illusion than the world of dreams.» Salvador Dali
|
|
|
|
|
1) Press F12 (while viewing this page)
2) copy the code below
3) paste it into the console and run it
let startTime = Date.now();
setTimeout(()=>{
let endTime= Date.now();
let timeSpent=(endTime-startTime)/1000+" seconds.";
alert("It took " + timeSpent + " to complete");
},Math.floor((Math.random() * 8000)));
It'll set a timeout that will last between 0 and 8 seconds.
When the timeout fires, it will calc times between startTime and endTime (in seconds) and display the value in an alert.
Does calculating the time between those two times not work?
It's an interesting coincidence because I just used the time measuring code in some JS I wrote yesterday.
There is also the new(er)
window.performance.now()
Update
Just noticed that it sounds like you are saying you "have a start and end time"...maybe as a string. that's a lot more difficult.
|
|
|
|
|
I think the "problem" with my code is that the value comes from an input field.
Your code seems to work, but I don't think I'll notice a time zone difference in seconds
|
|
|
|
|
Sander Rossel wrote: but I don't think I'll notice a time zone difference in seconds
Yeah, I was thinking that too, but I just wanted to provide some code that would show a difference in two times. Good luck with it.
|
|
|
|
|
I feel your pain. I avoid front end web work like the plague.
Real programmers use butterflies
|
|
|
|
|
That means a lot coming from someone who writes parsers, lexers and MIDI libraries!
|
|
|
|
|
And not a one with any javascript code in it!
Real programmers use butterflies
|
|
|
|
|
Sander Rossel wrote: 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
So is that 3 hours til you got to sleep? Or 4? Or 2? What does Javascript say?
|
|
|
|
|
JavaScript says "sweet dreams" before smothering me with a pillow.
|
|
|
|
|
|
|
What puns are part of - a sentence full of actors! (8)
"I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
"Common sense is so rare these days, it should be classified as a super power" - Random T-shirt
AntiTwitter: @DalekDave is now a follower!
|
|
|
|
|
"I didn't mention the bats - he'd see them soon enough" - Hunter S Thompson - RIP
|
|
|
|
|
a sentence is full of WORDs
a PLAY is full of actors
WORDPLAY - puns a part of
// TODO: Insert something here
|
|
|
|
|
Close enough!
You are up tomorrow.
"I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
"Common sense is so rare these days, it should be classified as a super power" - Random T-shirt
AntiTwitter: @DalekDave is now a follower!
|
|
|
|
|
I was thinking along the same line exactly - why it is 'only' close?
"The only place where Success comes before Work is in the dictionary." Vidal Sassoon, 1928 - 2012
|
|
|
|
|
Because punctuation is (nearly always) only there to confuse:
What puns are
part of - a sentence WORD
full of actors! PLAY
WORDPLAY
"I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
"Common sense is so rare these days, it should be classified as a super power" - Random T-shirt
AntiTwitter: @DalekDave is now a follower!
|
|
|
|