有没有一种方法来始终如一地将一个文件名,日期时间值,当文件名是有点"游离形式QUOT;? [英] Is there a way to consistently convert a filename into a datetime value when the filename is somewhat "free form"?

查看:97
本文介绍了有没有一种方法来始终如一地将一个文件名,日期时间值,当文件名是有点"游离形式QUOT;?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设文件名总是包含嵌入在其中的日期,但它不会是统一的,有没有办法来解析这样,始终转换文件名,日期时间?

Assuming the filename will always contain the date embedded within it, but that it won't be uniform, is there a way to parse such and consistently convert the filename into a DateTime?

更具体地,如果文件名总是具有相同的格式,如:

More specifically, if the filename always has the same format, such as:

NNNNN.YYYY-MM-DD.log

"NNNNN.YYYY-MM-DD.log"

...这是pretty的方便。

...this is pretty easy.

但是,如果文件名可以是任何以下格式:

But, if the filenames can be in any of the following formats:

NNN.YYYY-MM-DD.log或 NNNNN.YYYY-MM-DD.txt或 NNNNNNN.YYYY-MM-DD.bunyan - 甚至可能: NNNNN.MM-DD-YYYY.xlw -和: NNNNN.YYYY-DD-MM.zsj

"NNN.YYYY-MM-DD.log" or "NNNNN.YYYY-MM-DD.txt" or "NNNNNNN.YYYY-MM-DD.bunyan" -- and maybe even: "NNNNN.MM-DD-YYYY.xlw" -and: "NNNNN.YYYY-DD-MM.zsj"

- IOW,文件名总是包含psented以某种方式之日起重新$ P $,但除此之外,所有的赌注都关闭,有没有希望,或者没了

-- IOW, the filenames always contain the date represented in some fashion, but otherwise, all bets are off, is there hope, or nope?

推荐答案

没有,有没有希望,除非你可以匹配到一个已知的格式的扩展。

No, there's no hope unless you can match an extension up to a known format.

有些天/月的组合,你会得到幸运像2012-12-31。显然,31是不是一个月的价值,你可以推断至少日/月/ 4位数的年份组合。

Some days/month combinations you'll get lucky like 2012-12-31. Clearly, 31 isn't the month value and you can deduce at least the day/month/4-digit year combo.

由于阿列克谢提到的,一个2位数的一年进一步复杂化的问题。

As Alexei mentioned, a 2 digit year further complicates the problem.

作为一个方面说明,这是类似于SQL Server问题pretty的。这是罚款和花花公子,直到它的运行有不同的日期和时间格式期望:

As a side note, this is pretty similar to a SQL Server problem. This is fine and dandy until it's run with different date time format expectations:

SELECT CAST('2012-05-01'AS DATETIME)

根据不同的区域,这可能是2012年5月1日或2012年1月5日。 (请参阅本主题:<一href="http://stackoverflow.com/questions/10818796/error-converting-string-to-datetime-due-to-locale">Error将字符串转换为datetime由于语言环境)

Depending on the locale, this could be May 1, 2012 or January 5, 2012. (See this thread: Error converting string to datetime due to locale)

这篇关于有没有一种方法来始终如一地将一个文件名,日期时间值,当文件名是有点&QUOT;游离形式QUOT;?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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