How I learned to stop worrying & love The Error
-
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:
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:
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
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 thealert()
box. You can also see my code at plunkr[ -
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:
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:
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
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 thealert()
box. You can also see my code at plunkr[There is no such thing as a timestamp without a timezone. Does not matter how you do it. All programming languages. All databases. Even when it is not obvious. Even when it is not defined. Even if you just write it on a piece of paper. Myself I am not even sure if dates (not timestamps) exist without a timezone. Certainly my birthday is just as useable here as it is halfway around the world. But if I am supposed to get a something free from a international company on my birthday every year as a swag then when does it show up?
-
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:
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:
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
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 thealert()
box. You can also see my code at plunkr[raddevus wrote:
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.
I agree with whoever said,Programming is the act of debugging a blank page. Maybe Knuth?