Convert from string to DateTime in VB.Net

2

My application allows you to import content from an Excel file into a DataGridView, with the aim of then sending to save all the contents to a BD, one of the columns is date type but with the format "MM / dd / yyyy" , that date before sending it to the BD I enter it in a variable of type string , what I want is to change the format to "dd / MM / yyyy" and send it to the BD with that format, the code I use It is as follows:

Dim fecha As String = ""
fecha = fila.Cells("F1").Value
Dim formato As String = ""
formato = Format(CDate(fecha), "dd/MM/yyyy")
agregar.Parameters.AddWithValue("@fecha", Convert.ToDateTime(formato))
  

The error is sent to me when evaluating the date "09/13/2017"

    
asked by Mario Antonio Ruiz Carrillo 04.10.2017 в 19:42
source

4 answers

0

You could use DateTime.TryParseExact to get a date to then convert it to the format you want or you would have to go through your chain.

Dim fecha As DateTime   
DateTime.TryParseExact(fila.Cells("F1").Value, "MM/dd/yyyy", CultureInfo.CurrentCulture, DateTimeStyles.None, fecha)    
agregar.Parameters.AddWithValue("@fecha", fecha.ToString("dd/MM/yyyy"))

DEMO

Reference:

answered by 04.10.2017 в 19:59
0

If the date String value is "09/13/2017" (format dd/MM/yyyy ), you need to convert it to a suitable value that would be MM/dd/yyyy , you can do it using ParseExact() :

 Dim fecha = "09/13/2017"
 Dim fechaConvertida As DateTime = DateTime.ParseExact(fecha , "MM/dd/yyyy", CultureInfo.CurrentCulture, DateTimeStyles.None)

in this way you would get the value of the date in the correct format, which would be:

13/09/2017 12:00:00 a.m.

but you can also apply a format if you want the converted value to String, using the .ToString() method:

fechaConvertida.ToString("dd/MM/yyyy")

to get: 13/09/2017

according to your code would be done this way:

Dim fecha As String = ""
fecha = fila.Cells("F1").Value
Dim fechaConvertida As DateTime = DateTime.ParseExact(fecha , "MM/dd/yyyy", CultureInfo.CurrentCulture, DateTimeStyles.None)
agregar.Parameters.AddWithValue("@fecha", fechaConvertida.ToString())
    
answered by 04.10.2017 в 20:45
0

You have several answers on how to convert a String to DateTime . They are correct, but what happens in your situation is that you have a concept problem that is very common and that I will try to clarify in mine.

  

one of the columns is date type but with the format "MM / dd / yyyy"

No, it's not like that. The date fields, both in VB.Net and in the Databases, do not have a format . They are stored as a numerical value. For example, in SqlServer it is stored using 8 bytes, 4 for the date (the number of days is saved from January 1, 1900) and 4 for the time (number of ticks since midnight). The format is a representation of this data when it is transformed into a string.

Analyzing your code:

formato = Format(CDate(fecha), "dd/MM/yyyy")

Here what you do is convert a String to Datetime , to convert it back to String

agregar.Parameters.AddWithValue("@fecha", Convert.ToDateTime(formato))

and here, that String convert it back to Datetime . Why so much conversion?

This should be your code:

Dim fecha As String = ""
fecha = fila.Cells("F1").Value
agregar.Parameters.AddWithValue("@fecha", CDate(fecha))
    
answered by 05.10.2017 в 09:38
0

Dim fecha As String = fila.Cells("F1").Value
Dim formato As Date = Format(CDate(fecha), "dd/MM/yyyy")
Parameters.AddWithValue("@fecha", Convert.ToDateTime(formato))https://es.stackoverflow.com/posts/170881/edit#
Dim fecha As String = fila.Cells("F1").Value
Dim formato As Date = Format(CDate(fecha), "dd/MM/yyyy")
Parameters.AddWithValue("@fecha", Convert.ToDateTime(formato))

' No le pasabas un Formato de variable de Fecha...
    
answered by 06.06.2018 в 02:31