Missing time value in DateField

  1. #1

    Missing time value in DateField

    Hello,

    I have an <ext:Toolbar> with a DateField and a button:
    <ext:DateField ID="dtDesde" runat="server" Format="dd/MM/yyyy HH:mm" Width="125" />
    <ext:Button ID="Button1" runat="server" Text="Filtrar" Icon="DatabaseGo" OnDirectClick="Buscar_Evento" />
    When button is clicked, code-behind function Buscar_Evento is fired:
    protected void Buscar_Evento(object sender, EventArgs e)
    {
        DateTime fechaDesde = (DateTime)dtDesde.Value;
    }
    The problem is that, even when the dtDesde DateField control shows '24/09/2014 14:19' on the page, when the code-behind is invoked, dtDesde.Value has '24/09/2014 0:00:00' as value and time portion is lost.

    Any suggestion to get the time portion correctly?
    Thank you
    Regards
    Alex
  2. #2

    RE: Missing time value in DateField

    Well, I've solved this way:

    Instead getting the value in code-behind, I've passed dtDesde.getValue() as an ExtraParam of Button1 DirectEvent call.

    If there's a better way, let me know.
    Thanks
  3. #3
    Hi @aluna,

    That is an interesting question.

    Currently, the time part is being cut off at the stage of parsing the POST data. It is done in the setter of the DateField's SelectedDate property.
    public virtual DateTime SelectedDate
    {
        get
        {
            object obj = this.Value;
            return obj == null ? (DateTime)this.EmptyValue : (DateTime)obj;
        }
        set
        {
            this.Value = value.Date;
        }
    }
    Honestly, I don't know why it is done. Because the DateField name doesn't contain "time"? Well, it doesn't look the good reason to shrink the usage field of the DateField component. Though, there still might be another reason. I will discuss with my colleagues.

    ExtraParams is good to use.

    There is another possible approach.

    Example
    <%@ Page Language="C#" %>
    
    <script runat="server">
        protected void Submit(object sender, EventArgs e)
        {
            DateTime dt = DateTime.ParseExact(this.DateField1.RawText, this.DateField1.Format, System.Globalization.CultureInfo.InvariantCulture);
    
            X.Msg.Alert("Submitted", dt.ToShortDateString() + " " + dt.ToShortTimeString()).Show();
        }
    </script>
    
    <!DOCTYPE html>
    
    <html>
    <head runat="server">
        <title>Ext.NET v2 Example</title>
    </head>
    <body>
        <form runat="server">
            <ext:ResourceManager runat="server" />
            
            <ext:DateField ID="DateField1" runat="server" Format="dd/MM/yyyy HH:mm" Width="150" />
    
            <ext:Button runat="server" Text="Submit" OnDirectClick="Submit" />
        </form>
    </body>
    </html>
  4. #4
    Thank you @Daniil
  5. #5
    It looks like a DateField doesn't support a time part, because there is no "time" in its name:)

    If to be more serious, then a DateField is really not quite designed for time. Yes, you can set a Format with a time part and you can type it in the field. but you cannot choose a time part from a picker. If you are OK with that, you can go further with workarounds above. As for us, we probably leave the things as it is.

    By the way, there is a TimeField control. It might be more useful for an end user.

Similar Threads

  1. Getting time using DateField
    By aluna in forum 1.x Help
    Replies: 1
    Last Post: Sep 25, 2014, 8:06 PM
  2. Replies: 5
    Last Post: Feb 17, 2013, 12:51 AM
  3. DateField setting default time value
    By douglas in forum 1.x Help
    Replies: 0
    Last Post: Jan 24, 2013, 11:47 AM
  4. [CLOSED] JS format function of datefield is missing
    By Daly_AF in forum 2.x Legacy Premium Help
    Replies: 2
    Last Post: Nov 06, 2012, 8:47 AM
  5. Replies: 2
    Last Post: Mar 28, 2012, 1:39 PM

Tags for this Thread

Posting Permissions