且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何检查字符串是否包含日期?

更新时间:2022-04-13 19:35:00

日期没有格式.从MSDN:

Dates do not have a format. From MSDN:

表示时间瞬间,通常表示为日期和时间.
...
时间值以100纳秒为单位进行测量,称为刻度,特定的日期是GregorianCalendar日历中自0001 AD(CE)0001年1月1日午夜以来的刻度数...例如,刻度值 31241376000000000L 表示日期,即0100年1月1日,星期五,午夜12:00:00.

Represents an instant in time, typically expressed as a date and time of day.
...
Time values are measured in 100-nanosecond units called ticks, and a particular date is the number of ticks since 12:00 midnight, January 1, 0001 A.D. (C.E.) in the GregorianCalendar calendar...For example, a ticks value of 31241376000000000L represents the date, Friday, January 01, 0100 12:00:00 midnight.

因此, DateTime 只是一个很大的数字.将它们表示为"dd/MM/yyyy"是 DateTime 类型的魔术的一部分.问题的一部分是这样的:

So, a DateTime is just a Big Number. Representing them as "dd/MM/yyyy" is part of the magic of the DateTime type. Part of the issue is this:

Console.WriteLine(ds.Tables(0).Rows(x)(z).ToString())

行项目是 Object .除非/直到您将其放入 DateTime 变量中,否则它不会像 DateTime 类型那样工作.该打印为 DateTime 很简单,因为DataTable知道底层类型.但是它将为您的区域性使用默认格式.这使它看起来像 日期具有内置格式(如果您尝试将日期设置为某种格式,甚至可以更改格式"),但是您是人类,并且 635882810022222112L 对我们大多数人来说毫无意义.

Row items are Object. It wont act like a DateTime type unless/until you get it into a DateTime variable. That print as a DateTime simple because the DataTable knows the underlying type; but it will use the default format for your Culture. This makes it look like dates have a built in format (or even that the "format changed" if you tried to set it to something), but you are a human and 635882810022222112L would not make sense to most of us.

要更改输出样式,首先 需要将其放入 DateTime 变量中.显然,第一步是确定任意列是否为日期.而不是测试输出的格式",而是测试基础数据类型.这确实假定 DataTable 中的 DateTime 列正确:

To change the output style, you first need to get it into a DateTime variable. Apparently, a preliminary step is to determine if an arbitrary column is a Date. Rather than testing the "format" of the output, test the underlying data type. This does assume a proper DateTime column in the DataTable:

If ds.Tables(0).Columns(n).DataType = GetType(DateTime) Then
    '...
End If
' Or:
If ds.Tables(0).Rows(x)(z).GetType Is GetType(DateTime) Then
    '...
End If

然后更改显示,首先将其放入 DateTime 变量:

Then to change the display, first get it into a DateTime variable:

Dim dt As DateTime
If ds.Tables(0).Rows(x)(z).GetType Is GetType(DateTime) Then
    dt = Convert.ToDateTime(ds.Tables(0).Rows(x)(z))
    ' cant change "format" but you can change how it displays:
    Console.WriteLine(dt.ToLongDateString)
    Console.WriteLine(dt.ToString("yyyy-MM-dd HH:mm tt"))
    Console.WriteLine(dt.ToString("dd MMM, yyyy"))
End If

获取并转换为的更简单方法是使用 Field(Of T)扩展名:

Dim dt = ds.Tables(0).Rows(x).Field(Of DateTime)(y)


当我执行插入操作时通常执行以下操作:Date.Now.ToString("yyyy-MM-dd HH:mm:ss")所以我将格式应用于日期以插入...如果我不这样做的话按照我显示的格式正确格式化日期,我得到了这个值0000-00-00 00:00:00

这不适用于日期格式.它将 DateTime 转换为字符串.虽然"yyyy-MM-dd HH:mm:ss"是将日期数据作为字符串传递到MySql时使用的正确格式,但不需要.MySQL数据提供者知道如何将Net DateTime var转换为MySql需要/想要的数据,然后再次返回-这就是它的工作.

That doesn't apply a format to a date. It converts the DateTime to a string. While "yyyy-MM-dd HH:mm:ss" is the correct format to use when passing date data as a string to MySql, it is not needed. The MySQL Data provider knows how to convert a Net DateTime var to the data MySql needs/wants and back again -- that's its job.

' this will work fine
cmd.Parameters.Add("@SomeDate", MySqlDbType.DateTime).Value = myDateTimeVar

您了解的格式要求是您需要在MySql shell或WorkBench UI中使用的格式,因为您是在其中从键盘输入文本/字符串的.这并不意味着 code 必须将 DateTime 变量转换为特定格式的字符串以进行存储.

The format requirement you read about is the what you need to use in the MySql shell or WorkBench UI because you are entering text/string there...from the keyboard. It does not mean code must convert DateTime variables to string in a specific format for storing.