Display formatted Date in TextBox on Form

1 Answer 4115 Views
Form TextBox
Richard
Top achievements
Rank 4
Iron
Iron
Iron
Richard asked on 10 Dec 2021, 01:16 PM

Afternoon,

Is there a way to display a formatted date in a readonly TextBox on a kendo form without using a DateTimePicker as below?

            items.Add()

                .Field(f => f.Datelastedited)

                .Label(l => l.Text("Date Last Edited:"))

                .Editor(e => e.DateTimePicker()

                    .HtmlAttributes(new { style = "width: 100%", title = "datetimepicker" })

                    .Format("{0:dd/MM/yyyy HH:mm:ss tt}"))

                .InputHtmlAttributes(new { @readonly = "readonly" });

I want the TextBox to be readonly so I don't need the ability to change the value, or display the buttons to set the time or date elements.

A NumericTextBox has a Format method, but the TestBox doesn't.

Kind regards,

Richard

1 Answer, 1 is accepted

Sort by
1
Accepted
Aleksandar
Telerik team
answered on 15 Dec 2021, 06:43 AM

Hello Richard,

Indeed this would be the suggested way to format and display a readonly date.

An alternative I can suggest in case you desire to use a TextBox instead of a DateTimePicker is to add a Model property of type string and assign to it the formatted the Date object:

        public DateTime OrderDate
        {
            get;
            set;
        }

        public String OrderDateAsString => OrderDate.ToLongDateString();

You can then use this model property and display it in a TextBox.

I hope this helps.

Regards,
Aleksandar
Progress Telerik

Virtual Classroom, the free self-paced technical training that gets you up to speed with Telerik and Kendo UI products quickly just got a fresh new look + new and improved content including a brand new Blazor course! Check it out at https://learn.telerik.com/.

Richard
Top achievements
Rank 4
Iron
Iron
Iron
commented on 16 Dec 2021, 12:18 PM

Hi Aleksandar,

Many thanks for your suggestion - that has worked perfectly:

        public DateTime? Datelastedited { get; set; }

        public string DatelasteditedAsString => Datelastedited.Value.ToString("dd/MM/yyyy HH:mm:ss");

 And:

            items.Add()

                .Field(f => f.DatelasteditedAsString)

                .Label(l => l.Text("Date Last Edited:"))

                .Editor(e => e.TextBox())

                .InputHtmlAttributes(new { @readonly = "readonly" });

Kind regards,

Richard

Tags
Form TextBox
Asked by
Richard
Top achievements
Rank 4
Iron
Iron
Iron
Answers by
Aleksandar
Telerik team
Share this question
or