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. Add months and set the last day of the month in one step

Add months and set the last day of the month in one step

Scheduled Pinned Locked Moved C#
question
5 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.
  • S Offline
    S Offline
    sri_3464 0
    wrote on last edited by
    #1

    I want to add months and set the day as last day of that added month. I know i can do in the following way, DateTime ldtManipulatedDate = new DateTime(1998,lintMonths,1).AddMonths(lintAddMonths); ldtManipulatedDate = new DateTime(ldtManipulatedDate.Year ,ldtManipulatedDate.Months,DateTime.DaysInMonth( ldtManipulatedDate.Year,ldtManipulatedDate.Months)); The above code will first add months and then set the day as last day of that added month. But i want this in one step as i am instantiating two times. Please let me know how can i do it?

    L 2 Replies Last reply
    0
    • S sri_3464 0

      I want to add months and set the day as last day of that added month. I know i can do in the following way, DateTime ldtManipulatedDate = new DateTime(1998,lintMonths,1).AddMonths(lintAddMonths); ldtManipulatedDate = new DateTime(ldtManipulatedDate.Year ,ldtManipulatedDate.Months,DateTime.DaysInMonth( ldtManipulatedDate.Year,ldtManipulatedDate.Months)); The above code will first add months and then set the day as last day of that added month. But i want this in one step as i am instantiating two times. Please let me know how can i do it?

      L Offline
      L Offline
      Luc Pattyn
      wrote on last edited by
      #2

      Hi, from year and month:

      DateTime dt=new DateTime(year, month, 1).AddMonths(monthsToAdd+1).AddDays(-1);

      or from DateTime:

      dt=dt.AddMonths(monthToAdd+1).AddDays(-dt.Day); // incorrect at end of month
      dt=dt.AddDays(1-dt.Day).AddMonths(monthToAdd+1).AddDays(-1); // fixed

      :)

      Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]


      I only read code that is properly formatted, adding PRE tags is the easiest way to obtain that.
      [The QA section does it automatically now, I hope we soon get it on regular forums as well]


      modified on Wednesday, February 3, 2010 10:08 PM

      S 1 Reply Last reply
      0
      • S sri_3464 0

        I want to add months and set the day as last day of that added month. I know i can do in the following way, DateTime ldtManipulatedDate = new DateTime(1998,lintMonths,1).AddMonths(lintAddMonths); ldtManipulatedDate = new DateTime(ldtManipulatedDate.Year ,ldtManipulatedDate.Months,DateTime.DaysInMonth( ldtManipulatedDate.Year,ldtManipulatedDate.Months)); The above code will first add months and then set the day as last day of that added month. But i want this in one step as i am instantiating two times. Please let me know how can i do it?

        L Offline
        L Offline
        Luc Pattyn
        wrote on last edited by
        #3

        fixed a bug :)

        Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]


        I only read code that is properly formatted, adding PRE tags is the easiest way to obtain that.
        [The QA section does it automatically now, I hope we soon get it on regular forums as well]


        1 Reply Last reply
        0
        • L Luc Pattyn

          Hi, from year and month:

          DateTime dt=new DateTime(year, month, 1).AddMonths(monthsToAdd+1).AddDays(-1);

          or from DateTime:

          dt=dt.AddMonths(monthToAdd+1).AddDays(-dt.Day); // incorrect at end of month
          dt=dt.AddDays(1-dt.Day).AddMonths(monthToAdd+1).AddDays(-1); // fixed

          :)

          Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]


          I only read code that is properly formatted, adding PRE tags is the easiest way to obtain that.
          [The QA section does it automatically now, I hope we soon get it on regular forums as well]


          modified on Wednesday, February 3, 2010 10:08 PM

          S Offline
          S Offline
          sri_3464 0
          wrote on last edited by
          #4

          Thanks Luc!! Why didnt it strike in my mind... :)

          L 1 Reply Last reply
          0
          • S sri_3464 0

            Thanks Luc!! Why didnt it strike in my mind... :)

            L Offline
            L Offline
            Luc Pattyn
            wrote on last edited by
            #5

            you're welcome. :)

            Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]


            I only read code that is properly formatted, adding PRE tags is the easiest way to obtain that.
            [The QA section does it automatically now, I hope we soon get it on regular forums as well]


            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