If your time value (string) will ALWAYS be in HH:mm format, i.e., 5
characters long, 3rd character = ":", ist 2 characters are in the range "00"
to "23" and last 2 characters are in the range "00" to "59", then you can
use the following which will always work, regardless of culture:
Dim _d As DateTime = DateTime.ParseExact(MyTime, "HH:mm", nothing)
Dim MyNewDate As DateTime =
MyDate.Date.AddHours(_d.Hour).AddMinutes(_d.Minute)
"John Dann" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
>I have a standard .Net DateTime variable to which I want to add a time
> value. The catch is that the time value is only readily available as a
> string as "HH:mm".
>
> Question is whether there's a simple construction that would do this.
> I'm thinking of something along the lines of:
>
> Dim MyDate as Date = New Date(1, 1, 2007)
> Dim MyNewDate as Date
> Dim MyTime as String = "HH:mm"
>
> MyNewDate = Date.Parse(MyDate.ToString("??") & " " & MyTime)
>
> (Not sure what the ?? should be.)
>
> or maybe
>
> MyNewDate = Date.Parse(MyDate.ToShortDateString & " " & MyTime)
>
> might be the best option.
>
> I know I could deconstruct the HH:mm string into its constituent hours
> and minutes integer values, but I was just curious as to whether
> there's a more direct way of doing this. Another alternative might be
> if there's any way of parsing a Timespan type from the time string,
> but I can't spot any such method.
>
> NB This needs to work for locales using both US and non-US date
> formats, which is partly why I'm asking here, ie to get a solution
> that will definitely work worldwide and not just in my current locale.
> JGD
|