如何检查字符串是否包含日期? [英] How to check if a string contains a date?

查看:99
本文介绍了如何检查字符串是否包含日期?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试对 DataSet 进行迭代,其中包含查询结果,例如 SELECT * FROM tb 1 ,现在前三个字段包含日期,数据库表中保存的格式是这样:

I'm trying to iterate on a DataSet, this contain a results of query such as SELECT * FROM tb 1, now the first three field contains a date, the format saved in the database table is this:

yyyy-MM-dd HH:mm:ss

但代码返回以下内容:

yyyy/MM/dd HH:mm:ss

尤其是:

For z = 1 To ds.Tables(0).Columns.Count - 1

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

Next

因此,我需要识别当前字符串是否具有以下格式: yyyy/MM/dd HH:mm:ss 并将其解析为: yyyy-MM-dd HH:mm:ss 我很难接受 regex 模式来识别它,但是我不是regex的专家.无论如何,如果有其他解决方案,我将很高兴看到.请注意,表的前三个值和最后一个是日期,其他值不是日期,但包含整数或其他字符串值.

So I need to recognize if the current string have this format: yyyy/MM/dd HH:mm:ss and parse it into: yyyy-MM-dd HH:mm:ss I tough to a regex pattern for recognize it, but I'm not an expert of regex. Anyway, if there is another solution I'll glad to see. Note that only the first three value and the last one of the table is date, the other values aren't date but contain integer or other string value.

推荐答案

日期没有格式.从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.

这篇关于如何检查字符串是否包含日期?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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