Adding a string time value to a date variable

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • John Dann

    #1

    Adding a string time value to a date variable

    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(MyDa te.ToString("?? ") & " " & MyTime)

    (Not sure what the ?? should be.)

    or maybe

    MyNewDate = Date.Parse(MyDa te.ToShortDateS tring & " " & 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
  • Cor Ligthert [MVP]

    #2
    Re: Adding a string time value to a date variable

    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(MyTi me)
    ///

    I hope this helps,

    Cor

    "John Dann" <news@prodata.c o.ukschreef in bericht
    news:g4s9s2h1u0 d5ksovd5q7dcc4d pcgkeq2qn@4ax.c om...
    >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(MyDa te.ToString("?? ") & " " & MyTime)
    >
    (Not sure what the ?? should be.)
    >
    or maybe
    >
    MyNewDate = Date.Parse(MyDa te.ToShortDateS tring & " " & 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

    Comment

    • Stephany Young

      #3
      Re: Adding a string time value to a date variable

      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.ParseE xact(MyTime, "HH:mm", nothing)

      Dim MyNewDate As DateTime =
      MyDate.Date.Add Hours(_d.Hour). AddMinutes(_d.M inute)


      "John Dann" <news@prodata.c o.ukwrote in message
      news:g4s9s2h1u0 d5ksovd5q7dcc4d pcgkeq2qn@4ax.c om...
      >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(MyDa te.ToString("?? ") & " " & MyTime)
      >
      (Not sure what the ?? should be.)
      >
      or maybe
      >
      MyNewDate = Date.Parse(MyDa te.ToShortDateS tring & " " & 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

      Comment

      Working...