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 / C++ / MFC
  4. How to calculate a date one moth before from Current date?

How to calculate a date one moth before from Current date?

Scheduled Pinned Locked Moved C / C++ / MFC
helptutorialquestion
14 Posts 6 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.
  • M megha_gharote

    Dear Friends, I need to display a date exact month before from current date. For Ex: todays date is 14-OCt-08.. then i should display 14-Sep-08. Can anybody please help me out with this? Thank u so much in advance Megha

    CPalliniC Offline
    CPalliniC Offline
    CPallini
    wrote on last edited by
    #4

    I think the below code will do the trick, anyway verifying it is up to you :rolleyes: CTime today = CTime::GetCurrentTime();
    CTime aMonthAgo;
    CTimeSpan ts(today.GetDay(),0,0,0);
    aMonthAgo = today - ts;
    aMonthAgo += CTimeSpan( - aMonthAgo.GetDay() + today.GetDay(), 0, 0, 0);
    [added #1] That was buggy, forget it :-O . Anyway are you aware that your requirement cannot be satisfied when current day is for instance March 31th? [/added #1] [added #2] Fixed code:

    CTime today = CTime::GetCurrentTime();
    CTime aMonthAgo;
    CTimeSpan ts( today.GetDay(), 0, 0, 0 );
    aMonthAgo = today - ts;
    int iSpan = aMonthAgo.GetDay() - today.GetDay();
    if (iSpan > 0)
    aMonthAgo -= CTimeSpan( iSpan, 0, 0, 0);

    :) This code sets aMonthAgo to: (1) a date having the same day (of month) of current date, but month equal to previous month, whenever such requirement can be satisfied. (2) Last day of previous month whenever the requirement (1) cannot be satisfied (for instance when input date is March 31th). :)

    If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
    This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
    [My articles]

    modified on Tuesday, October 14, 2008 6:48 AM

    In testa che avete, signor di Ceprano?

    1 Reply Last reply
    0
    • M megha_gharote

      This is what i did till now.. CTime currentTime=CTime::GetCurrentTime(); int nDay = currentTime.GetDay(); int nMonth = currentTime.GetMonth(); int nYear = currentTime.GetYear(); int nHour = currentTime.GetHour(); int nMin = currentTime.GetMinute(); int nSec = currentTime.GetSecond(); currentTime -= 2592000; ///30*24*60*60 (Seconds) nDay = currentTime.GetDay(); nMonth = currentTime.GetMonth(); nYear = currentTime.GetYear(); nHour = currentTime.GetHour(); nMin = currentTime.GetMinute(); nSec = currentTime.GetSecond(); But i dont want to calculate it like this. Can anybody suggest me soem other method?

      R Offline
      R Offline
      Rajesh R Subramanian
      wrote on last edited by
      #5

      Try this instead:

      SYSTEMTIME varTime;
      GetSystemTime(&varTime);
      varTime.wMonth--;

      After the third statement, the month would be decremented by one. Kinda obvious, but you asked for it. Add: Oops! I haven't taken account of the year January. But I think that you must be able to do it yourself now. :-\

      Many are stubborn in pursuit of the path they have chosen, few in pursuit of the goal - Friedrich Nietzsche .·´¯`·->Rajesh<-·´¯`·. [Microsoft MVP - Visual C++]

      N 1 Reply Last reply
      0
      • M megha_gharote

        Dear Friends, I need to display a date exact month before from current date. For Ex: todays date is 14-OCt-08.. then i should display 14-Sep-08. Can anybody please help me out with this? Thank u so much in advance Megha

        enhzflepE Offline
        enhzflepE Offline
        enhzflep
        wrote on last edited by
        #6
        SYSTEMTIME today, lastMonth;
        
        // get current time and make a duplicate of the information
        GetLocalTime(&today);
        memcpy(&lastMonth, &today, sizeof(SYSTEMTIME));
        
        // as long as it's not january, 1 month ago is still in this year
        if (lastMonth.wMonth > 1)
            lastMonth.wMonth--;
        
        // set month to december of last year
        else
        {
            lastMonth.wMonth = 12;
            lastMonth.wYear--;
        }
        
        R 1 Reply Last reply
        0
        • enhzflepE enhzflep
          SYSTEMTIME today, lastMonth;
          
          // get current time and make a duplicate of the information
          GetLocalTime(&today);
          memcpy(&lastMonth, &today, sizeof(SYSTEMTIME));
          
          // as long as it's not january, 1 month ago is still in this year
          if (lastMonth.wMonth > 1)
              lastMonth.wMonth--;
          
          // set month to december of last year
          else
          {
              lastMonth.wMonth = 12;
              lastMonth.wYear--;
          }
          
          R Offline
          R Offline
          Rajesh R Subramanian
          wrote on last edited by
          #7

          enhzflep wrote:

          // as long as it's not january,

          My 5 for your attention to detail. I missed out on the case where the month could be January! :)

          Many are stubborn in pursuit of the path they have chosen, few in pursuit of the goal - Friedrich Nietzsche .·´¯`·->Rajesh<-·´¯`·. [Microsoft MVP - Visual C++]

          enhzflepE 1 Reply Last reply
          0
          • R Rajesh R Subramanian

            enhzflep wrote:

            // as long as it's not january,

            My 5 for your attention to detail. I missed out on the case where the month could be January! :)

            Many are stubborn in pursuit of the path they have chosen, few in pursuit of the goal - Friedrich Nietzsche .·´¯`·->Rajesh<-·´¯`·. [Microsoft MVP - Visual C++]

            enhzflepE Offline
            enhzflepE Offline
            enhzflep
            wrote on last edited by
            #8

            Oh gee Raj, thanks.:rose: Quick question - do I lose thhose points for forgetting to consider the question: What is the date 1 month prior to the 30th of march?

            CPalliniC R 2 Replies Last reply
            0
            • enhzflepE enhzflep

              Oh gee Raj, thanks.:rose: Quick question - do I lose thhose points for forgetting to consider the question: What is the date 1 month prior to the 30th of march?

              CPalliniC Offline
              CPalliniC Offline
              CPallini
              wrote on last edited by
              #9

              That's a flaw of OP's requirements. The same applies to March 31th (and, for most years, 29th), May 31th,... :)

              If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
              This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
              [My articles]

              In testa che avete, signor di Ceprano?

              1 Reply Last reply
              0
              • enhzflepE enhzflep

                Oh gee Raj, thanks.:rose: Quick question - do I lose thhose points for forgetting to consider the question: What is the date 1 month prior to the 30th of march?

                R Offline
                R Offline
                Rajesh R Subramanian
                wrote on last edited by
                #10

                As CPallini said. :)

                Many are stubborn in pursuit of the path they have chosen, few in pursuit of the goal - Friedrich Nietzsche .·´¯`·->Rajesh<-·´¯`·. [Microsoft MVP - Visual C++]

                1 Reply Last reply
                0
                • R Rajesh R Subramanian

                  Try this instead:

                  SYSTEMTIME varTime;
                  GetSystemTime(&varTime);
                  varTime.wMonth--;

                  After the third statement, the month would be decremented by one. Kinda obvious, but you asked for it. Add: Oops! I haven't taken account of the year January. But I think that you must be able to do it yourself now. :-\

                  Many are stubborn in pursuit of the path they have chosen, few in pursuit of the goal - Friedrich Nietzsche .·´¯`·->Rajesh<-·´¯`·. [Microsoft MVP - Visual C++]

                  N Offline
                  N Offline
                  Nelek
                  wrote on last edited by
                  #11

                  What about if you are on 30th or 31st of march? Or any other 31st where the moth before only 30 has? Won't it crash? :P EDIT: Question was already below, I should read all before posting :doh:

                  Regards. -------- M.D.V. ;) If something has a solution... Why do we have to worry about?. If it has no solution... For what reason do we have to worry about? Help me to understand what I'm saying, and I'll explain it better to you “The First Rule of Program Optimization: Don't do it. The Second Rule of Program Optimization (for experts only!): Don't do it yet.” - Michael A. Jackson Rating helpfull answers is nice, but saying thanks can be even nicer.

                  R 2 Replies Last reply
                  0
                  • N Nelek

                    What about if you are on 30th or 31st of march? Or any other 31st where the moth before only 30 has? Won't it crash? :P EDIT: Question was already below, I should read all before posting :doh:

                    Regards. -------- M.D.V. ;) If something has a solution... Why do we have to worry about?. If it has no solution... For what reason do we have to worry about? Help me to understand what I'm saying, and I'll explain it better to you “The First Rule of Program Optimization: Don't do it. The Second Rule of Program Optimization (for experts only!): Don't do it yet.” - Michael A. Jackson Rating helpfull answers is nice, but saying thanks can be even nicer.

                    R Offline
                    R Offline
                    Rajesh R Subramanian
                    wrote on last edited by
                    #12

                    That was discussed two posts below here[^]. But that is an exercise that the OP must take care of. :)

                    Many are stubborn in pursuit of the path they have chosen, few in pursuit of the goal - Friedrich Nietzsche .·´¯`·->Rajesh<-·´¯`·. [Microsoft MVP - Visual C++]

                    1 Reply Last reply
                    0
                    • N Nelek

                      What about if you are on 30th or 31st of march? Or any other 31st where the moth before only 30 has? Won't it crash? :P EDIT: Question was already below, I should read all before posting :doh:

                      Regards. -------- M.D.V. ;) If something has a solution... Why do we have to worry about?. If it has no solution... For what reason do we have to worry about? Help me to understand what I'm saying, and I'll explain it better to you “The First Rule of Program Optimization: Don't do it. The Second Rule of Program Optimization (for experts only!): Don't do it yet.” - Michael A. Jackson Rating helpfull answers is nice, but saying thanks can be even nicer.

                      R Offline
                      R Offline
                      Rajesh R Subramanian
                      wrote on last edited by
                      #13

                      Nelek wrote:

                      What about if you are on 30th or 31st of march? Or any other 31st where the moth before only 30 has? Won't it crash?

                      BTW, I forgot to mention it won't *Crash* anyways. SYSTEMTIME is just a structure and it can store the value you put into it. The trouble may come, only when a call to something like SetTime() is called with the new time structure after decrement has happened. :)

                      Many are stubborn in pursuit of the path they have chosen, few in pursuit of the goal - Friedrich Nietzsche .·´¯`·->Rajesh<-·´¯`·. [Microsoft MVP - Visual C++]

                      1 Reply Last reply
                      0
                      • M megha_gharote

                        Dear Friends, I need to display a date exact month before from current date. For Ex: todays date is 14-OCt-08.. then i should display 14-Sep-08. Can anybody please help me out with this? Thank u so much in advance Megha

                        D Offline
                        D Offline
                        David Crow
                        wrote on last edited by
                        #14

                        If the current date happens to be March 31st and the year is not a leap year, the most days you'd be off is 3. That said, once you subtract 1 from the current month (use the % operator so that it will wrap back around to December as needed), you may also need to subtract up to 3 days to have a valid date.

                        "Love people and use things, not love things and use people." - Unknown

                        "The brick walls are there for a reason...to stop the people who don't want it badly enough." - Randy Pausch

                        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