Give a specific format to a date field in a gridview

2

I am using a gridview in ASP.NET and I am loading it with a sql statement and I activate the function of selecting the row

<asp:GridView ID="GridView1" runat="server" AllowPaging="True" AutoGenerateColumns="False" BorderColor="Black" DataSourceID="SqlDataSource1" OnSelectedIndexChanged="GridView1_SelectedIndexChanged" BorderStyle="Groove" OnRowCommand="GridView1_RowCommand" Width="956px">
    <Columns>
        <asp:CommandField ShowSelectButton="True" />
        <asp:BoundField DataField="PatientiID" HeaderText="PatientiID" SortExpression="PatientiID" />
        <asp:BoundField DataField="OccurTime" HeaderText="OccurTime" SortExpression="OccurTime" />
        <asp:BoundField DataField="AlarmFlag" HeaderText="AlarmFlag" SortExpression="AlarmFlag" />
        <asp:BoundField DataField="AlarmSeverity" HeaderText="AlarmSeverity" SortExpression="AlarmSeverity" />
        <asp:BoundField DataField="Description1" HeaderText="Description1" SortExpression="Description1" />
    </Columns>
</asp:GridView>

and I try to get the OccurTime data with the following method

 protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
        {
            int P = 0;
            String Fecha;

            GridViewRow row = GridView1.SelectedRow;

            Label3.Text = row.Cells[1].Text;
            Label2.Text = row.Cells[2].Text;
            P = int.Parse(row.Cells[1].Text);
            Fecha = row.Cells[2].Text;
            Label4.Text = Fecha;
            Grafica_Chart(P,Fecha);
        }

I need to send the date in a string in the way '2007-05-08 12:35:00' to the Grafica_Chart method but it brings it to me '08 / 05/2007 12:35:00 p. m. '

How could that format change?

    
asked by Sebastian Mateus Villegas 30.09.2016 в 16:08
source

1 answer

1

Assuming that row.Cells[2].Text contains a string of the "08/05/2007 12:35:00 p.m." format, then you can parse date to a DateTime using the static method ParseExact() that this class provides then convert it to the format you want .

CultureInfo provider = CultureInfo.InvariantCulture;
DateTime fecha = DateTime.ParseExact(row.Cells[2].Text,"dd/MM/yyyy mm:hh:ss tt",provider);  

String.Format("{0:yyyy-MM-dd hh:mm:ss}",fecha);

Or directly with the ToString()

fecha.ToString("yyyy-MM-dd hh:mm:ss");

Check if the date is parseable

You may want to check that the date is parseable or not, since ParseExact() if the string is not valid will throw an exception, then you have the wonderful DateTime.TryParseExact() and you can use it this way:

string fecha = "08/05/2007 12:35:00 pm";

//DateTime f =  DateTime.ParseExact(fecha, "dd/MM/yyyy hh:mm:ss tt", CultureInfo.InvariantCulture);
DateTime result;
bool parseado = DateTime.TryParseExact(fecha, 
                                       "dd/MM/yyyy hh:mm:ss tt", 
                                       CultureInfo.InvariantCulture, 
                                       DateTimeStyles.None, 
                                       out result);

if (parseado)
    //aquí va el código que utiliza la fecha ya 

What this static method of DateTime does is try to convert the fecha using the format string "dd/MM/yyyy hh:mm:ss tt" and, if you could convert it, place it in the variable result . This method lets you know if you could convert it or not because it returns a value bool indicating it.

You can check:

Strings with format

ParseExact

    
answered by 30.09.2016 в 16:23