简体   繁体   中英

Working with Excel Dates in vb.net

I am experiencing a problem when attempting to read a date from an excel sheet. (The date column is formatted the same as the short date format of the computer its opened on). I populate the dates from the excel sheet into a datagrid with success, but when I attempt to parse the date (To format it appropriately), I get a error saying the string wasn't a valid DateTime value. The computer's short date format is dd/MM/yyyy. This is the code I use to try parsing the date. The following code is an example of where the process fails.

Dim dateParsed AS DateTime = DateTime.Parse("14/01/2013").ToString("yyyy-MM-dd")

Is there some way to programatically get the system's short date format and use ParseExact instead or any suggestions?

You want to parse exact, using the current cultures short-date format?

Dim dt As DateTime = DateTime.ParseExact(str, "d", CultureInfo.CurrentCulture)

Additionally, if you actually need to see the ShortDate format for yourself get it through the CurrentCulture.

CultureInfo.CurrentCulture.DateTimeFormat.ShortDatePattern

You can try this piece of coding

      Dim dt As DateTime = DateTime.Parse("1/14/2013").ToString

      Dim f As String = Format(dt, "yyyy-MM-dd")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM