2

I want to assign value to my HTML tag input type="date" from C# codebehind in ASP.NET web application. After page is loaded, the value is not visible, but in chrome dev tools i can see the value:

enter image description here

ASPX:

<input type="date" runat="server" id="date_datumPrispetja" value="dd. MM. yyyy" />

Codebehind:

string date = myDate.ToString("dd. MM. yyyy"); //value of date: "09. 04. 2017";
date_datumPrispetja.Value = date;

Is this is even possible, to assign value from codebehind to this HTML5 element?


EDIT:

Compare on datetime and date input type. C#:

date_datumPrispetja.Value = DateTime.Now.ToString("dd. MM. yyyy");
hi.Value = DateTime.Now.ToString("dd. MM. yyyy");

ASPX:

<input type="date"  runat="server" id="date_datumPrispetja" value="dd. MM. yyyy"/>
<br />
<input type="datetime" runat="server" id="hi" value="dd. MM. yyyy" />

RESULT:

enter image description here

tadej
  • 701
  • 1
  • 5
  • 22

5 Answers5

11

I think it should be work, where did you put this code in:

string date = myDate.ToString("dd. MM. yyyy"); //value of date: "09. 04. 2017";
date_datumPrispetja.Value = date;

Page_Load()?

My test code as bellow:

protected void Page_Load(object sender, EventArgs e)
    {
        hi.Value = DateTime.Now.ToString("yyyy-MM-dd");
    }

Then it worked well.

Justin Shi
  • 304
  • 2
  • 6
  • Can you show me your `` element definition in your aspx, please? – tadej Apr 21 '17 at 09:05
  • I need a date picker in same input, so datetime doesnt work for me. – tadej Apr 21 '17 at 09:10
  • The type is doesn't matter. I changed the type to Date & still get the current experiences. – Justin Shi Apr 21 '17 at 09:25
  • And from the screenshot you provided, the value actually is correct but not showing in the browser, so looks like it's not refresh the latest value in browser? – Justin Shi Apr 21 '17 at 09:31
  • 2
    Aha, I found the root cause. You should use:DateTime.Now.ToString("yyyy-MM-dd"); rather than DateTime.Now.ToString("dd. MM. yyyy"); in Page_Load. See more details from: http://stackoverflow.com/questions/14212527/how-to-set-default-value-to-the-inputtype-date – Justin Shi Apr 21 '17 at 10:32
  • Yes! It works, thanks for all your help, maybe you should edit the main answer, so it will be correct. – tadej Apr 21 '17 at 10:44
2

This Worked for me.

TextBox.Text = Convert.ToDateTime('01-01-2020').Date.ToString("yyyy-MM-dd");
1

Mobile Safari, Firefox and Chromium accept the date value only in format

<input type="date" value="2017-04-27">

and will print the date in the localized format. If they get a localized format as input (e.g. "27.04.2017") they print placeholders only.

However Safari accepts

<input type="date" value="27.04.2017">

On the other hand if Safari get the input format "2017-04-27", it will print this value in a non localized form.

Inside the browser's DOM the input value is stored attribute defaultValue (true for all browsers) and the attribute value is empty (not true for Safari).

My workaround for this confusion is some javascript:

$(function () {
    var datefields = $('.TTMMJJ')
    for (i = 0; i < datefields.length; i++) {
        datefields[i].type = "date";
        var defaultVal = datefields[i].defaultValue;
        var val = datefields[i].value;
        if (defaultVal.length > 0 && val.length == 0) {
            var darr = defaultVal.split(".");
            if (darr.length == 3) {
                var year = darr[2];
                var month = darr[1];
                var day = darr[0];
                if (year.length == 2) {
                    year = "20" + year;
                }
                var s = year + '-' + month.padStart(2,0) + '-' + day.padStart(2,0);
                datefields[i].value = s;
            }
        }
    }
});

Note that

Date.parse("27.04.2017") 

works with Chromium but not with Firefox.

Frank Hintsch
  • 560
  • 8
  • 14
0

This works for me:

txtFechaOtorgamiento.Text = Format(servicio.FechaOtorgamiento, "yyyy-MM-dd");
0

In this case the format of date string should be "yyyy-MM-dd" as following: date.Value = DateTime.Now.ToString("yyyy-MM-dd");