Home

Date Formats with TextBox of type date.

I am trying to create a TextBox with the type set to Date.

I have it working, but the date formatting seems a little inconsistent.

If I have code like this

    var textBox1 = new TextBox("");
    textBox1.HtmlElement["type"] = "date";
    textBox1.Dump();

    new Button("Continue", (a) => textBox1.Text.Dump("Selected Date")).Dump();;

Then I get output like

Which tells me what the date format is (and as I am based in the UK, that is what I would expect).

But if I select today's date the contents of the textbox is displayed as 08/02/2022, but the Text property is set to '2022-02-08'

i.e.

If I want to initialise the date time box, I need to do something like

    var dt = new DateTime(2022, 12, 25);

    var textBox1 = new TextBox("");
    textBox1.HtmlElement["type"] = "date";
    textBox1.HtmlElement["value"] = dt.ToString("yyyy-MM-dd");
    textBox1.Dump();

It works, but is a little strange to have to used two different date formats and I was wondering if I am missing something?

Comments

Sign In or Register to comment.