DateTime.ParseExact未被识别为有效的DateTime [英] DateTime.ParseExact was not recognized as a valid DateTime

查看:475
本文介绍了DateTime.ParseExact未被识别为有效的DateTime的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用以下代码将 String 转换为 Date 格式:

I'm trying to convert a String to a Date format using this code:

 Debug.Print(Trim(oTable.Cell(i + 1, 2).Range.Text.Replace(vbCr, "").Replace(vbLf, "")))
 Tabledate = DateTime.ParseExact((Trim(oTable.Cell(i + 1, 2).Range.Text.Replace(vbCr, "").Replace(vbLf, ""))), "dd/MM/yy", CultureInfo.InvariantCulture)

Debug.Print 行给我的结果 30/12/15 ,但 DateTime.ParseExact 行总是返回一个错误,表示字符串不是有效的DateTime格式?我是否缺少某些东西?

the Debug.Print line gives me the result 30/12/15, but the DateTime.ParseExact line always returns an error saying the string was not a valid DateTime format? Am I missing something?

注意:我必须删除尾随的返回carragies,因为字符串是从一个表中出来的,并且由于某种原因,在字符串的结尾

Note: I have to remove trailing return carragies because the string is coming from a table in word, and for some reason pulls out a return on the end of the string

更新1

采取绝望措施,在我的代码中包含以下内容:

Resorting to desperate measures, I've included the following in my code:

For Each A As Char In oTable.Cell(i + 1, 2).Range.Text
    Debug.Print(A)
Next

这是打印结果:

3
0
/
1
2
/
1
5
(extra blank char)
(extra blank char)

我不知道这些空白字符在哪里或什么,我已经尝试替换 vbTab 没有任何东西,但没有什么区别。可能值得注意的是,String最初是从一个表中出来的,表中的一个单元格可能与这些空白字符有什么关系吗?

I have no idea where or what these blank characters are, I've tried replacing vbTab with nothing too but it makes no difference. It might be worth noting that the String originally comes from a Table in word, could the fact its in a cell on a table have anything to do with what these blank characters are?

更新2

将调试代码更改为:

For Each A As Char In oTable.Cell(i + 1, 2).Range.Text
    Debug.Print(CInt(AscW(A)))
Next

返回:

51
48
47
49
50
47
49
53
13
7

我相信13是一个回车(即使它应该被修剪掉),而7是bell(我不知道是什么铃声)

which I believe means 13 is a carriage return (even though it should have been trimmed out) and 7 is a "bell" (I have no idea what a bell is)

推荐答案

旧的VB函数 修剪 只能从头和尾移除空格。使用.NET方法 String.Trim ,而不是删除所有类型的 whitespaces ,也可以是tab-characters。

The old VB function Trim removes only spaces from the beginning and the end. Use the .NET method String.Trim instead which removes all kind of whitespaces like also tab-characters.

Dim celltext =  oTable.Cell(i + 1, 2).Range.Text.Replace(vbCr, "").Replace(vbLf, "")
Tabledate = DateTime.ParseExact(celltext.Trim(), "dd/MM/yy", CultureInfo.InvariantCulture)

请注意,您还可以使用 DateTime.TryParseExact 如果输入可以包含无效的日期。然后没有异常发生,但该方法返回 False

Note that you also could use DateTime.TryParseExact if the input can contain invalid dates. Then no exception happens but the method returns False.

你有回车(不是在结束)和钟(结束)你可能想使用这种方法,应该删除所有不需要的字符:

Since you have the carriage return(not at the end) and the bell(at the end) you might want to use this approach which should remove all unwanted characters:

Dim celltext = oTable.Cell(i + 1, 2).Range.Text.Trim()
Dim validChars = From c In celltext Where Char.IsDigit(c) OrElse Char.IsPunctuation(c)
celltext = New String(validChars.ToArray())
Tabledate = DateTime.ParseExact(celltext, "dd/MM/yy", CultureInfo.InvariantCulture)

您需要将 Imports System.Linq 添加到代码文件的顶部。

You need to add Imports System.Linq to the top of the code file.

这篇关于DateTime.ParseExact未被识别为有效的DateTime的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆