Click here to Skip to main content
15,867,308 members

The Weird and The Wonderful

   

The Weird and The Wonderful forum is a place to post Coding Horrors, Worst Practices, and the occasional flash of brilliance.

We all come across code that simply boggles the mind. Lazy kludges, embarrassing mistakes, horrid workarounds and developers just not quite getting it. And then somedays we come across - or write - the truly sublime.

Post your Best, your worst, and your most interesting. But please - no programming questions . This forum is purely for amusement and discussions on code snippets. All actual programming questions will be removed.

 
GeneralRe: ChatGPT "solves" a riddle for me. Pin
PIEBALDconsult13-Mar-23 5:34
mvePIEBALDconsult13-Mar-23 5:34 
GeneralRe: ChatGPT "solves" a riddle for me. Pin
PIEBALDconsult13-Mar-23 13:34
mvePIEBALDconsult13-Mar-23 13:34 
GeneralRe: ChatGPT "solves" a riddle for me. Pin
adriancs6-Apr-23 1:15
mvaadriancs6-Apr-23 1:15 
GeneralRe: ChatGPT "solves" a riddle for me. Pin
adriancs6-Apr-23 1:26
mvaadriancs6-Apr-23 1:26 
GeneralRe: ChatGPT "solves" a riddle for me. Pin
DrWalter PE26-Apr-23 18:38
professionalDrWalter PE26-Apr-23 18:38 
GeneralRe: ChatGPT "solves" a riddle for me. Pin
GKP199226-Apr-23 20:26
professionalGKP199226-Apr-23 20:26 
GeneralRe: ChatGPT "solves" a riddle for me. Pin
Amarnath S18-May-23 1:08
professionalAmarnath S18-May-23 1:08 
GeneralHow I learned to stop worrying & love The Error Pin
raddevus12-Mar-23 12:48
mvaraddevus12-Mar-23 12:48 
I've just painfully (while developing an app) stumbled upon this terrible little bug in the HTML5 Date control.

TLDR;
If you set the value of your HTML5 date input control to today's date, it will display that date.
However, when you go to read the date using the same property of the input control that you used to set it, you __may__ get a date that is one day less than the date you set!!!!
Code and snapshots below...

If you add the basic Date input control to your page:
HTML
<input type="date" id="mainDate">

You'll see something like what is shown in this snapshot[^].

Notice that the date is not set.

To set the Date to the current date you need JavaScript (of course).
So do something like the following:

JavaScript
document.querySelector("#mainDate").valueAsDate = new Date();


Now the Date control will be set to the current date for you, based upon your local date/time.

The control looks like the following snapshot for me now[^].

Let's Read the Value Back, Shall We?

Let's add some code to read the value back -- even though it is obvious that the value will be 03-12-2023, right? Right? Right!?! Insert maniacal laughter here!!!

Here's the Extremely Simple JavaScript Code
JavaScript
function showDate(){
    alert(document.querySelector("#mainDate").valueAsDate);
  }


The Date Reported is One Day Behind
You can see in this snapshot that my system actually[^] reports that it is not 03-12-2023 (it actually is IRL (In Real Life), but it reports it as a time on 03-11-2023.

Inspect My Simple Code At JSFiddle
If you can't believe my snapshots, take a look at the code[^] and you can see that I simply read the valueAsDate from the date input control and pop up the value in the alert() box.

You can also see my code at plunkr[^]. I noticed jsfiddle was down so I added the code there.

I Came, I Saw, I Searched
yes, I searched for something like, "why is my HTML5 date control one day off?"
I found this highly upvoted StackOverflow question: https://stackoverflow.com/questions/9509360/datepicker-date-off-by-one-day[^]

The Answer Is Far Deeper Than You Want
Of course there is a logical explanation, but I don't care about that when I'm attemptingn to build an app and get it working properly!!! Roll eyes | :rolleyes:

There are multiple answers that state that it is related to the data format containing dashes - or slashes / but I believe the correct answer is found in a comment and it's quite terrible (as all date-related code is).

SO Commentor said
This isn't a bug. When you provide the date in standard UTC format, Date assumes that you are providing a UTC time, which is independent of your local time. The reason you're off by day is because UTC time is for a timezone that is ahead of yours. (Since you didn't also provide a time, 0;0;0 was assumed.) Supplying a date in a format other than UTC indicates use of a local timezone. I happened across this problem in another context, with YAML interpreting my UTC format date off by a day.


Learned to Love The Bugs
When I was about 3 years into my career I understood that more of software development would be about debugging than actually putting the bugs in (writing code) so I gave in and I've learned to love the bugs.

I can remember the exact instant when I realized that a large part of my life from then on was going to be spent in finding mistakes in my own programs.


These kinds of things make you so angry that they remind you that you are actually alive, not just an inanimate lump of clay sitting in a cubicle for 72 hours a week. Laugh | :laugh:

"I debug therefore I am."

modified 12-Mar-23 22:51pm.

GeneralRe: How I learned to stop worrying & love The Error Pin
jschell20-Mar-23 7:13
jschell20-Mar-23 7:13 
GeneralRe: How I learned to stop worrying & love The Error Pin
DrWalter PE26-Apr-23 18:38
professionalDrWalter PE26-Apr-23 18:38 
GeneralBut it feels so _diiirtyyy_! Pin
PIEBALDconsult1-Mar-23 8:01
mvePIEBALDconsult1-Mar-23 8:01 
GeneralRe: But it feels so _diiirtyyy_! Pin
Mircea Neacsu1-Mar-23 8:34
Mircea Neacsu1-Mar-23 8:34 
GeneralRe: But it feels so _diiirtyyy_! Pin
PIEBALDconsult1-Mar-23 9:08
mvePIEBALDconsult1-Mar-23 9:08 
GeneralRe: But it feels so _diiirtyyy_! Pin
Mircea Neacsu1-Mar-23 9:22
Mircea Neacsu1-Mar-23 9:22 
GeneralRe: But it feels so _diiirtyyy_! Pin
PIEBALDconsult1-Mar-23 9:32
mvePIEBALDconsult1-Mar-23 9:32 
GeneralRe: But it feels so _diiirtyyy_! Pin
jschell20-Mar-23 7:20
jschell20-Mar-23 7:20 
GeneralRe: But it feels so _diiirtyyy_! Pin
PIEBALDconsult20-Mar-23 10:04
mvePIEBALDconsult20-Mar-23 10:04 
GeneralRe: But it feels so _diiirtyyy_! Pin
jschell21-Mar-23 5:35
jschell21-Mar-23 5:35 
GeneralRe: But it feels so _diiirtyyy_! Pin
PIEBALDconsult21-Mar-23 5:50
mvePIEBALDconsult21-Mar-23 5:50 
GeneralRe: But it feels so _diiirtyyy_! Pin
jschell23-Mar-23 5:55
jschell23-Mar-23 5:55 
GeneralRe: But it feels so _diiirtyyy_! Pin
PIEBALDconsult23-Mar-23 6:53
mvePIEBALDconsult23-Mar-23 6:53 
GeneralChatGTP: Write me a poem about programming Pin
Marc Clifton1-Feb-23 3:00
mvaMarc Clifton1-Feb-23 3:00 
GeneralRe: ChatGTP: Write me a poem about programming Pin
Sean Ewington1-Feb-23 3:02
staffSean Ewington1-Feb-23 3:02 
GeneralRe: ChatGTP: Write me a poem about programming Pin
Slacker00716-Feb-23 1:13
professionalSlacker00716-Feb-23 1:13 
GeneralRe: ChatGTP: Write me a poem about programming Pin
Andre Oosthuizen28-Feb-23 2:03
mveAndre Oosthuizen28-Feb-23 2:03 

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.