Convert HiddenField Value to Datetime in C #

0

Greetings,

I have the following HiddenField

<asp:HiddenField ID="hdfFecha" runat="server" />

and in the code behing I have, to convert it to "dd-MM-yyyy" format.

FormAGPost.Fecha = DateTime.ParseExact(hdfFecha.Value, "dd-MM-yyyy", CultureInfo.InvariantCulture);

But I get an error when converting it. This is the error that gives me:

  

An exception of type 'System.NullReferenceException' occurred in CentroEntrevista.dll but was not handled in user code Additional information: Object reference not set to an instance of an object.

the value obtained in the variable is: "21-01-2017T00: 00: 00 + 00: 00"

    
asked by Oscar Diaz 11.01.2017 в 16:11
source

2 answers

0

Convert it to DateTime first.

FormAGPost.Fecha = Convert.ToDateTime(hdfFecha.Value).ToString("dd-MM-yyyy");
    
answered by 11.01.2017 в 16:23
-1

The DateTime.Parse method is used to convert a text format to a date format.

FormAGPost.Date = DateTime.Parse (hdfFecha.Value.ToString ()). ToString ("dd-MM-yyyy");

Post the error you give so that the answer is more accurate.

    
answered by 11.01.2017 в 16:35