简体   繁体   English

在vb.net中使用Excel日期

[英]Working with Excel Dates in vb.net

I am experiencing a problem when attempting to read a date from an excel sheet. 尝试从Excel工作表中读取日期时遇到问题。 (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. 我成功地将Excel工作表中的日期填充到了一个数据网格中,但是当我尝试解析该日期(以适当地设置其格式)时,我收到一条错误消息,指出该字符串不是有效的DateTime值。 The computer's short date format is dd/MM/yyyy. 计算机的短日期格式为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? 有什么办法以编程方式获取系统的短日期格式,并使用ParseExact代替或提供任何建议?

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. 此外,如果您实际需要自己查看ShortDate格式,请通过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")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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