System.Convert.ToDateTime(string) As Date
Hi All
I have written a code in visual basic 2005 to compare between dates
If
CDate(tocomparewith_1) >= d1 And CDate(tocomparewith_2) <= d2 Thentocomparewith_1 & tocomparewith_2 are String formats.
d1 and d2 are Date formats.
The string and date are in the following format "MM/dd/yyyy H:mm"
now no matter if i use Cdate(tocomparewith_1) or System.Convert.ToDateTime(tocomparewith_1)
It returns an error if tocomparewith_1 value is "12/23/2007 6:23"
i guess Cdate or System.Convert.ToDateTime needs the date in format "dd/MM/yyyy H:mm"
caues it works fine in that way.
Now the problem is how do i present Cdate or System.Convert.ToDateTime with a string in that format.
Also i havent heard of any function that allows me to specify the existing format of date along with the convert to date format. cause if i use the format( ) function to covert to "dd/MM/yyyy H:mm" it doest give me consistent results.
eg
Existing Format / Date "MM/dd/yyyy" / "06/09/2007"
On one system it reads it as 06 sep 2007 on other system it reads the same date as june 09 2007
Thanks in advance for your time.
Respuestas
- The parsing is probably due to the current culture settings. If you want to provide a particular date format regardless of culture, take a look at the DateTime.ParseExact methdod:
http://msdn2.microsoft.com/en-us/library/w2sa9yss(vs.80).aspx
The example in the article is not very helpful, but the method should provide what you need. Try the following:
Function String2Date(ByVal DDMMYY As String) As DateTime
Return DateTime.Parse(DDMMYY, New System.Globalization.CultureInfo("en-GB"))
End FunctionFor other date time functions, please feel free to visit my blog here:
http://petersgyoung.spaces.live.com/blog/cns!706591A544AC0876!138.entry
Todas las respuestas
- The parsing is probably due to the current culture settings. If you want to provide a particular date format regardless of culture, take a look at the DateTime.ParseExact methdod:
http://msdn2.microsoft.com/en-us/library/w2sa9yss(vs.80).aspx
The example in the article is not very helpful, but the method should provide what you need. Try the following:
Function String2Date(ByVal DDMMYY As String) As DateTime
Return DateTime.Parse(DDMMYY, New System.Globalization.CultureInfo("en-GB"))
End FunctionFor other date time functions, please feel free to visit my blog here:
http://petersgyoung.spaces.live.com/blog/cns!706591A544AC0876!138.entry
Thank you guys for ur help..
- Thank you
it so esay to learn but to do .................. more buges in very where
than you agin

