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. Other Discussions
  3. The Weird and The Wonderful
  4. How NOT to calculate the end of month...

How NOT to calculate the end of month...

Scheduled Pinned Locked Moved The Weird and The Wonderful
ruby
14 Posts 9 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.
  • R Rob Grainger

    Actually, he had unit tests in place, but all the dates tested were on the first day of the month. My fix was:

    return new DateTime(dt.Year, dt.Month, 1).AddMonths(1).AddDays(-1);

    "If you don't fail at least 90 percent of the time, you're not aiming high enough." Alan Kay.

    L Offline
    L Offline
    Lost User
    wrote on last edited by
    #5

    And why not simply use DateTime.DaysInMonth?

    It does not solve my Problem, but it answers my question

    K 1 Reply Last reply
    0
    • L Lost User

      And why not simply use DateTime.DaysInMonth?

      It does not solve my Problem, but it answers my question

      K Offline
      K Offline
      kalberts
      wrote on last edited by
      #6

      Because the programmer wanted the attention of being presented here? :-)

      L 1 Reply Last reply
      0
      • K kalberts

        Because the programmer wanted the attention of being presented here? :-)

        L Offline
        L Offline
        Lost User
        wrote on last edited by
        #7

        Makes sense :laugh:

        It does not solve my Problem, but it answers my question

        1 Reply Last reply
        0
        • R Rob Grainger

          Actually, he had unit tests in place, but all the dates tested were on the first day of the month. My fix was:

          return new DateTime(dt.Year, dt.Month, 1).AddMonths(1).AddDays(-1);

          "If you don't fail at least 90 percent of the time, you're not aiming high enough." Alan Kay.

          M Offline
          M Offline
          Marc Clifton
          wrote on last edited by
          #8

          Rob Grainger wrote:

          but all the dates tested were on the first day of the month.

          :laugh: :laugh: :laugh:

          Latest Articles:
          Abusing Extension Methods, Null Continuation, and Null Coalescence Operators

          1 Reply Last reply
          0
          • R Rob Grainger

            Found this gem in our codebase...

            public static class DateTimeExtensions
            {
            public static DateTime EndOfMonth(this DateTime dt)
            {
            return dt.Date.AddMonths(1).AddDays(-1);
            }
            }

            "If you don't fail at least 90 percent of the time, you're not aiming high enough." Alan Kay.

            E Offline
            E Offline
            EbenRoux
            wrote on last edited by
            #9

            Not to be pernickity but I guess it is going to depend on the intention of the method. If a trial runs for a month then perhaps from a given date that would be the end of the month :) However, the intention is not clear from the method name so one may very well assume that it represents the date for the last day in the month of the given date.

            R 1 Reply Last reply
            0
            • E EbenRoux

              Not to be pernickity but I guess it is going to depend on the intention of the method. If a trial runs for a month then perhaps from a given date that would be the end of the month :) However, the intention is not clear from the method name so one may very well assume that it represents the date for the last day in the month of the given date.

              R Offline
              R Offline
              Rob Grainger
              wrote on last edited by
              #10

              Personally, I feel that an extension method on DateTime should apply to all possible DateTime instances. Otherwise, it can be defined to match circumstances, as long as that is clear from the name.

              "If you don't fail at least 90 percent of the time, you're not aiming high enough." Alan Kay.

              1 Reply Last reply
              0
              • R Rob Grainger

                Found this gem in our codebase...

                public static class DateTimeExtensions
                {
                public static DateTime EndOfMonth(this DateTime dt)
                {
                return dt.Date.AddMonths(1).AddDays(-1);
                }
                }

                "If you don't fail at least 90 percent of the time, you're not aiming high enough." Alan Kay.

                B Offline
                B Offline
                BDieser
                wrote on last edited by
                #11

                30 days have September, all the rest I can't remember.

                R 1 Reply Last reply
                0
                • R Rob Grainger

                  Actually, he had unit tests in place, but all the dates tested were on the first day of the month. My fix was:

                  return new DateTime(dt.Year, dt.Month, 1).AddMonths(1).AddDays(-1);

                  "If you don't fail at least 90 percent of the time, you're not aiming high enough." Alan Kay.

                  O Offline
                  O Offline
                  obermd
                  wrote on last edited by
                  #12

                  Rob Grainger wrote:

                  return new DateTime(dt.Year, dt.Month, 1).AddMonths(1).AddDays(-1);

                  This is exactly my EndOfMonth extension function in dotNet.

                  1 Reply Last reply
                  0
                  • B BDieser

                    30 days have September, all the rest I can't remember.

                    R Offline
                    R Offline
                    Rob Grainger
                    wrote on last edited by
                    #13

                    I resemble that remark.

                    "If you don't fail at least 90 percent of the time, you're not aiming high enough." Alan Kay.

                    1 Reply Last reply
                    0
                    • R Rob Grainger

                      Actually, he had unit tests in place, but all the dates tested were on the first day of the month. My fix was:

                      return new DateTime(dt.Year, dt.Month, 1).AddMonths(1).AddDays(-1);

                      "If you don't fail at least 90 percent of the time, you're not aiming high enough." Alan Kay.

                      M Offline
                      M Offline
                      Myron Dombrowski
                      wrote on last edited by
                      #14

                      This gets you the last *date* of the month, but if you’re actually looking for the “end” of the month and don’t want fencepost errors, you’d want the first of the next month (with no time component) and then use a strict less-than in your comparisons.

                      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