Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Calculate the TotalMonths difference between two DateTime

Calculate the TotalMonths difference between two DateTime

Scheduled Pinned Locked Moved C#
question
3 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • P Offline
    P Offline
    Patrick Klug
    wrote on last edited by
    #1

    just wondering if someone has implemented something that calculates the TotalMonths (or Weeks or Years) between two DateTime values. The TimeSpan class obviously provides things like TotalDays, TotalMinutes and the like but as it doesn't have any reference to a DateTime it can't give you the TotalMonths, TotalWeeks or TotalYears. has anyone done that already?

    D P 2 Replies Last reply
    0
    • P Patrick Klug

      just wondering if someone has implemented something that calculates the TotalMonths (or Weeks or Years) between two DateTime values. The TimeSpan class obviously provides things like TotalDays, TotalMinutes and the like but as it doesn't have any reference to a DateTime it can't give you the TotalMonths, TotalWeeks or TotalYears. has anyone done that already?

      D Offline
      D Offline
      darkelv
      wrote on last edited by
      #2

      It doesn't probably because "month" is rather ambiguous. Is it 28 days if the starting date fall in Feb, or the ending date fall in Feb? Or Feb is in the middle of the data range? Probably the same for "Year". How about leap years? For weeks, you can divide total days by 7.

      1 Reply Last reply
      0
      • P Patrick Klug

        just wondering if someone has implemented something that calculates the TotalMonths (or Weeks or Years) between two DateTime values. The TimeSpan class obviously provides things like TotalDays, TotalMinutes and the like but as it doesn't have any reference to a DateTime it can't give you the TotalMonths, TotalWeeks or TotalYears. has anyone done that already?

        P Offline
        P Offline
        Patrick Klug
        wrote on last edited by
        #3

        just discovered that the (VB.NET) DateAndTime class has a method called DateDiff which calculates such things...

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups