比较Vb.net中的字符串日期 [英] Compare string dates in Vb.net
问题描述
您好,在我的程序中,我将字符串中的日期转换为我的应用程序。我想比较这些字符串,看看日期之间是否有适当的差异。例如date1 =2015/02/20 12:12: 000date2 =08/02/2015 16:15:000我怎么能比较起始者只有年份值而不解析字符串到日期。谢谢。
我试过了。长度 - 但没有运气。
Hi in my program i am converting the dates in string for my application purposes.I want to compare those strings to see if the dates have the appropriate difference between them.Example date1 = "07/02/2015 12:12:000" date2 = "08/02/2015 16:15:000" how can i compare for starters only the year value without parsing the string to date.Thanks in advance.
I have tried with .Length -something but with no luck at all.
推荐答案
不要。
将字符串解析为DateTime,并比较年份值从那里。
否则,你必须处理日期格式的所有可能的变化:
Don't.
Parse the strings to DateTime, and compare the year value from there.
Otherwise, you have to cope will all the possible variatiuons on a date format:
19/02/2014
19/02/14
14/02/19
2014/02/19
02/19/14
...
全部各种文化中同一日期的有效版本。
使用Parse允许系统使用当前的文化信息开始,以便对实际日期格式进行良好猜测。
如果你必须使用字符串,那么看看String.Substring方法只提取年份 - 假设它总是在同一个地方......
Are all valid versions of the same date in various cultures.
Using Parse allows the system to use the current culture information to start with for a "good guess" as to the actual date format.
If you must play with strings, then look at the String.Substring method to extract just the year - assuming it is always in the same place...
这篇关于比较Vb.net中的字符串日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!