PC Review


Reply
Thread Tools Rate Thread

Adding a string time value to a date variable

 
 
John Dann
Guest
Posts: n/a
 
      3rd Feb 2007
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
 
Reply With Quote
 
 
 
 
Cor Ligthert [MVP]
Guest
Posts: n/a
 
      3rd Feb 2007
John,,

In my idea comes this the most near your question.

\\\
Dim MyDate As Date = New Date(2007, 1, 1)
Dim MyNewDate As Date
Dim MyTime As New TimeSpan(1, 30, 0)
MyNewDate = MyDate.Add(MyTime)
///

I hope this helps,

Cor

"John Dann" <(E-Mail Removed)> schreef in bericht
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



 
Reply With Quote
 
Stephany Young
Guest
Posts: n/a
 
      3rd Feb 2007
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


 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: get actual date from variable text string NickHK Microsoft Excel Programming 0 20th Dec 2006 04:42 AM
Re: get actual date from variable text string Jon Peltier Microsoft Excel Programming 0 19th Dec 2006 03:12 PM
Initializing a DateTime variable with a string date Gary Rynearson Microsoft Dot NET Framework 4 12th Dec 2005 05:36 PM
Adding quotes to the ends of a string variable Torben Laursen Microsoft Excel Programming 2 13th Nov 2004 11:37 AM
Date and time text string to date variable John Marshall, MVP Microsoft Access Getting Started 4 28th Jan 2004 01:01 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 04:51 AM.