Subtracting Dates

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

    #1

    Subtracting Dates

    I want to subtract two Date variables and show the result in this format:
    hh:mm:ss

    I'm thinking that there might be an easy way of doing that. something like:
    ....
    Return DateDiff(DateIn terval.Second, date1, date2).ToString ("hh:mm:ss")
    ....

    Is there such an easy way of doing this?
  • Jay B. Harlow [MVP - Outlook]

    #2
    Re: Subtracting Dates

    Amjad
    I normally use DateTime.Subtra ct to subtract dates:

    Dim date1 As DateTime = #10:30:00 AM#
    Dim date2 As DateTime = #9:30:00 AM#
    Dim ts As TimeSpan = date1.Subtract( date2)

    To get to hh:mm:ss specifically (or other custom date/time formats) I
    normally convert the TimeSpan to a date.

    Dim r As DateTime = DateTime.MinVal ue.Add(ts)
    Dim s As String = r.ToString("hh: mm:ss")

    TimeSpan.ToStri ng will optionally include any days & fraction of seconds in
    the converted value.

    NOTE: You need to make sure the TimeSpan is positive for the
    DateTime.MinVal ue.Add method to work.

    Hope this helps
    Jay



    "Amjad" <Amjad@discussi ons.microsoft.c om> wrote in message
    news:5E13AB1C-939B-40D1-B0E8-C2364F801D3C@mi crosoft.com...
    |I want to subtract two Date variables and show the result in this format:
    | hh:mm:ss
    |
    | I'm thinking that there might be an easy way of doing that. something
    like:
    | ...
    | Return DateDiff(DateIn terval.Second, date1, date2).ToString ("hh:mm:ss")
    | ...
    |
    | Is there such an easy way of doing this?


    Comment

    • Amjad

      #3
      Re: Subtracting Dates

      Thanks Jay! That answered my question.

      I have a follow-up question, and that is how can I add up the time periods
      of the format "HH:mm:ss" and report the result in the same format?



      "Jay B. Harlow [MVP - Outlook]" wrote:
      [color=blue]
      > Amjad
      > I normally use DateTime.Subtra ct to subtract dates:
      >
      > Dim date1 As DateTime = #10:30:00 AM#
      > Dim date2 As DateTime = #9:30:00 AM#
      > Dim ts As TimeSpan = date1.Subtract( date2)
      >
      > To get to hh:mm:ss specifically (or other custom date/time formats) I
      > normally convert the TimeSpan to a date.
      >
      > Dim r As DateTime = DateTime.MinVal ue.Add(ts)
      > Dim s As String = r.ToString("hh: mm:ss")
      >
      > TimeSpan.ToStri ng will optionally include any days & fraction of seconds in
      > the converted value.
      >
      > NOTE: You need to make sure the TimeSpan is positive for the
      > DateTime.MinVal ue.Add method to work.
      >
      > Hope this helps
      > Jay
      >
      >
      >
      > "Amjad" <Amjad@discussi ons.microsoft.c om> wrote in message
      > news:5E13AB1C-939B-40D1-B0E8-C2364F801D3C@mi crosoft.com...
      > |I want to subtract two Date variables and show the result in this format:
      > | hh:mm:ss
      > |
      > | I'm thinking that there might be an easy way of doing that. something
      > like:
      > | ...
      > | Return DateDiff(DateIn terval.Second, date1, date2).ToString ("hh:mm:ss")
      > | ...
      > |
      > | Is there such an easy way of doing this?
      >
      >
      >[/color]

      Comment

      • Jay B. Harlow [MVP - Outlook]

        #4
        Re: Subtracting Dates

        Amjad,
        Have you tried using TimeSpan.Add to keep a running total of "time"?

        Dim date1 As DateTime = #12:00:00 PM#
        Dim date2 As DateTime = #8:00:00 AM#
        Dim ts1 As TimeSpan = date1.Subtract( date2)

        Dim date3 As DateTime = #5:00:00 PM#
        Dim date4 As DateTime = #1:00:00 PM#
        Dim ts2 As TimeSpan = date3.Subtract( date4)

        Dim total As TimeSpan = ts1.Add(ts2)


        Then convert the total to a string.

        Dim r As DateTime = DateTime.MinVal ue.Add(total)
        Dim s As String = r.ToString("hh: mm:ss")

        Hope this helps
        Jay

        "Amjad" <Amjad@discussi ons.microsoft.c om> wrote in message
        news:3B969477-89FF-4A55-B226-4B353BCBFFDD@mi crosoft.com...
        | Thanks Jay! That answered my question.
        |
        | I have a follow-up question, and that is how can I add up the time periods
        | of the format "HH:mm:ss" and report the result in the same format?
        |
        |
        |
        | "Jay B. Harlow [MVP - Outlook]" wrote:
        |
        | > Amjad
        | > I normally use DateTime.Subtra ct to subtract dates:
        | >
        | > Dim date1 As DateTime = #10:30:00 AM#
        | > Dim date2 As DateTime = #9:30:00 AM#
        | > Dim ts As TimeSpan = date1.Subtract( date2)
        | >
        | > To get to hh:mm:ss specifically (or other custom date/time formats) I
        | > normally convert the TimeSpan to a date.
        | >
        | > Dim r As DateTime = DateTime.MinVal ue.Add(ts)
        | > Dim s As String = r.ToString("hh: mm:ss")
        | >
        | > TimeSpan.ToStri ng will optionally include any days & fraction of seconds
        in
        | > the converted value.
        | >
        | > NOTE: You need to make sure the TimeSpan is positive for the
        | > DateTime.MinVal ue.Add method to work.
        | >
        | > Hope this helps
        | > Jay
        | >
        | >
        | >
        | > "Amjad" <Amjad@discussi ons.microsoft.c om> wrote in message
        | > news:5E13AB1C-939B-40D1-B0E8-C2364F801D3C@mi crosoft.com...
        | > |I want to subtract two Date variables and show the result in this
        format:
        | > | hh:mm:ss
        | > |
        | > | I'm thinking that there might be an easy way of doing that. something
        | > like:
        | > | ...
        | > | Return DateDiff(DateIn terval.Second, date1,
        date2).ToString ("hh:mm:ss")
        | > | ...
        | > |
        | > | Is there such an easy way of doing this?
        | >
        | >
        | >


        Comment

        Working...