VB
An object-oriented programming language developed by Microsoft that is implemented on the .NET Framework. Previously known as Visual Basic .NET.
2,768 questions
This browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
Hello, I am loading a date from an excel file that has the format of 11/29/2021 however when I load it into excel it becomes 11/29/2021/ 5:04am for an example
Below is my code.
Dim dataReader As OleDbDataReader
Dim Path As String = "C:\Users\glennb\Documents\MyExcelFile.xlsx"
'This connection string states that there are no headers in the Excel file (HDR=NO) as in your example, change to (HDR=YES) if there are headers.
Dim connectionString As String = String.Format("Provider=Microsoft.ACE.OLEDB.12.0;Data Source={0};Extended Properties='Excel 12.0 Xml;HDR=NO'", Path)
'Create a connection object, and open it. It will be disposed at the end of the using statement
Using connection As New OleDbConnection(connectionString)
connection.Open()
'Create the SQL SELECT statement (note the use of $ at the end of the sheet name, this is a MUST)
Dim selectExpired As String = "SELECT * FROM [Sheet1$c1:A60]"
Using selectCommand As New OleDbCommand(selectExpired, connection)
'Execute the SELECT statement and assign to a DataReader
dataReader = selectCommand.ExecuteReader
Do While dataReader.Read
txtExpired.AppendText(dataReader(2).ToString & vbCrLf)
Loop
'Close the reader
dataReader.Close()
End Using
End Using
The data shows I just do not want the time stamp which is not in my excel file
Cast the string value for the date time to a DateTime then format it e.g.
Dim value = CDate("6/23/2021 2:39:40 PM")
Debug.WriteLine(value.ToString("MM/dd/yyyy"))
Debug.WriteLine(value.ToShortDateString())
We get
06/23/2021
6/23/2021
If unsure if the string can be converted use DateTime.TryParse