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. DateAdd equivalent?

DateAdd equivalent?

Scheduled Pinned Locked Moved C / C++ / MFC
c++question
19 Posts 3 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.
  • N nm_114

    Is there a c++ equivalent of vbs's DateAdd where you can add or subtract any amount of hours, days, months etc. to a date? - thanks

    K Offline
    K Offline
    kasturi_haribabu
    wrote on last edited by
    #4

    See if it helps you ... int nDay; int nMonth; int nYear; struct tm *when; time_t now, result; time( &now ); when = localtime( &now );//conversion from UTC to system time if(when == NULL)//Error in the calculation of time return -1; when->tm_mday = when->tm_mday + nDays; result = mktime( when ); if(result == (time_t)-1)//size of time_t = 0;result=-1--->error in calculation of result { return -1; } else { nDay = when->tm_mday; nMonth = when->tm_mon+1; nYear = when->tm_year+1900; } impossible to understand

    1 Reply Last reply
    0
    • N nm_114

      DavidCrow wrote:

      If you are using MFC, see the COleDateTime class.

      Nope, pure Win32 :(

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

      Ok. How are you obtaining the date value you want to manipulate?


      "Approved Workmen Are Not Ashamed" - 2 Timothy 2:15

      "Judge not by the eye but by the heart." - Native American Proverb

      N 1 Reply Last reply
      0
      • D David Crow

        Ok. How are you obtaining the date value you want to manipulate?


        "Approved Workmen Are Not Ashamed" - 2 Timothy 2:15

        "Judge not by the eye but by the heart." - Native American Proverb

        N Offline
        N Offline
        nm_114
        wrote on last edited by
        #6

        GetSystemTime() (then converting it to local time).

        D 1 Reply Last reply
        0
        • N nm_114

          GetSystemTime() (then converting it to local time).

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

          So do you have a SYSTEMTIME or a FILETIME object at this point?


          "Approved Workmen Are Not Ashamed" - 2 Timothy 2:15

          "Judge not by the eye but by the heart." - Native American Proverb

          N 1 Reply Last reply
          0
          • D David Crow

            So do you have a SYSTEMTIME or a FILETIME object at this point?


            "Approved Workmen Are Not Ashamed" - 2 Timothy 2:15

            "Judge not by the eye but by the heart." - Native American Proverb

            N Offline
            N Offline
            nm_114
            wrote on last edited by
            #8

            SYSTEMTIME.

            SYSTEMTIME stDateIWantToAddDaysOrMonthsTo;
            FILETIME ft, ftLocal;
            GetSystemTimeAsFileTime(&ft);
            FileTimeToLocalFileTime(&ft, &ftLocal);
            FileTimeToSystemTime(&ftLocal, &stDateIWantToAddDaysOrMonthsTo);

            D 1 Reply Last reply
            0
            • N nm_114

              SYSTEMTIME.

              SYSTEMTIME stDateIWantToAddDaysOrMonthsTo;
              FILETIME ft, ftLocal;
              GetSystemTimeAsFileTime(&ft);
              FileTimeToLocalFileTime(&ft, &ftLocal);
              FileTimeToSystemTime(&ftLocal, &stDateIWantToAddDaysOrMonthsTo);

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

              Ok, so what problem(s) are you having with "add or subtract any amount of hours, days, months etc. to a date?"


              "Approved Workmen Are Not Ashamed" - 2 Timothy 2:15

              "Judge not by the eye but by the heart." - Native American Proverb

              N 1 Reply Last reply
              0
              • D David Crow

                Ok, so what problem(s) are you having with "add or subtract any amount of hours, days, months etc. to a date?"


                "Approved Workmen Are Not Ashamed" - 2 Timothy 2:15

                "Judge not by the eye but by the heart." - Native American Proverb

                N Offline
                N Offline
                nm_114
                wrote on last edited by
                #10

                I don't know of any function that does this. I was looking for something like VBScript's DateAdd where I could do something like DateAdd(Month, 24, &stMyDate) and it would add 24 months to the date (2 years), taking into account leap years etc..

                D 1 Reply Last reply
                0
                • N nm_114

                  I don't know of any function that does this. I was looking for something like VBScript's DateAdd where I could do something like DateAdd(Month, 24, &stMyDate) and it would add 24 months to the date (2 years), taking into account leap years etc..

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

                  nm_114 wrote:

                  I don't know of any function that does this.

                  Why would you need a function, when one statement will work:

                  stDateIWantToAddDaysOrMonthsTo.wYear += 2;

                  You could roll your own function to this I suppose. Take a stroll through the other date/time functions to see what is available to you.


                  "Approved Workmen Are Not Ashamed" - 2 Timothy 2:15

                  "Judge not by the eye but by the heart." - Native American Proverb

                  N 1 Reply Last reply
                  0
                  • D David Crow

                    nm_114 wrote:

                    I don't know of any function that does this.

                    Why would you need a function, when one statement will work:

                    stDateIWantToAddDaysOrMonthsTo.wYear += 2;

                    You could roll your own function to this I suppose. Take a stroll through the other date/time functions to see what is available to you.


                    "Approved Workmen Are Not Ashamed" - 2 Timothy 2:15

                    "Judge not by the eye but by the heart." - Native American Proverb

                    N Offline
                    N Offline
                    nm_114
                    wrote on last edited by
                    #12

                    DavidCrow wrote:

                    Why would you need a function, when one statement will work:

                    It was a bad example I guess. It needs to be arbitrary. Like DateAdd(Month, 309, mydate), DateAdd(Day, -909, mydate) etc. I'm not sure if I'm explaining it right.

                    DavidCrow wrote:

                    Take a stroll through the other date/time functions to see what is available to you.

                    I did. I didn't see anything like DateAdd.

                    D 1 Reply Last reply
                    0
                    • N nm_114

                      DavidCrow wrote:

                      Why would you need a function, when one statement will work:

                      It was a bad example I guess. It needs to be arbitrary. Like DateAdd(Month, 309, mydate), DateAdd(Day, -909, mydate) etc. I'm not sure if I'm explaining it right.

                      DavidCrow wrote:

                      Take a stroll through the other date/time functions to see what is available to you.

                      I did. I didn't see anything like DateAdd.

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

                      If you convert the SYSTEMTIME structure to a FILETIME structure, you can add/subtract such values.


                      "Approved Workmen Are Not Ashamed" - 2 Timothy 2:15

                      "Judge not by the eye but by the heart." - Native American Proverb

                      N 1 Reply Last reply
                      0
                      • D David Crow

                        If you convert the SYSTEMTIME structure to a FILETIME structure, you can add/subtract such values.


                        "Approved Workmen Are Not Ashamed" - 2 Timothy 2:15

                        "Judge not by the eye but by the heart." - Native American Proverb

                        N Offline
                        N Offline
                        nm_114
                        wrote on last edited by
                        #14

                        :confused: How? I found an article at http://www.codeproject.com/datetime/winapi_datetime_ops.asp[^] but it doesn't work with months/years, and I have no idea if it works right with leap years etc.

                        D 1 Reply Last reply
                        0
                        • N nm_114

                          :confused: How? I found an article at http://www.codeproject.com/datetime/winapi_datetime_ops.asp[^] but it doesn't work with months/years, and I have no idea if it works right with leap years etc.

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

                          nm_114 wrote:

                          How?

                          By using SystemTimeToFileTime().

                          nm_114 wrote:

                          I found an article at http://www.codeproject.com/datetime/winapi\_datetime\_ops.asp\[^\]

                          That article is exactly what you need.

                          nm_114 wrote:

                          but it doesn't work with months/years

                          That's because a month does not have a constant value (i.e., 28, 29, 30, or 31 days) like the other items do.


                          "Approved Workmen Are Not Ashamed" - 2 Timothy 2:15

                          "Judge not by the eye but by the heart." - Native American Proverb

                          N 1 Reply Last reply
                          0
                          • D David Crow

                            nm_114 wrote:

                            How?

                            By using SystemTimeToFileTime().

                            nm_114 wrote:

                            I found an article at http://www.codeproject.com/datetime/winapi\_datetime\_ops.asp\[^\]

                            That article is exactly what you need.

                            nm_114 wrote:

                            but it doesn't work with months/years

                            That's because a month does not have a constant value (i.e., 28, 29, 30, or 31 days) like the other items do.


                            "Approved Workmen Are Not Ashamed" - 2 Timothy 2:15

                            "Judge not by the eye but by the heart." - Native American Proverb

                            N Offline
                            N Offline
                            nm_114
                            wrote on last edited by
                            #16

                            DavidCrow wrote:

                            nm_114 wrote: How? By using SystemTimeToFileTime().

                            Well I knew that much! :-D I was asking how you do the adding/subtracting...

                            DavidCrow wrote:

                            That article is exactly what you need.

                            Well not really, as it doesn't handle months/years.

                            DavidCrow wrote:

                            That's because a month does not have a constant value (i.e., 28, 29, 30, or 31 days) like the other items do.

                            I knew that too! :-D But it's ok, I guess what I was looking for doesn't exist (a tested/bug-free exact replica of DateAdd() in C++). Oh well :sigh:. Thanks anyway.

                            D 1 Reply Last reply
                            0
                            • N nm_114

                              DavidCrow wrote:

                              nm_114 wrote: How? By using SystemTimeToFileTime().

                              Well I knew that much! :-D I was asking how you do the adding/subtracting...

                              DavidCrow wrote:

                              That article is exactly what you need.

                              Well not really, as it doesn't handle months/years.

                              DavidCrow wrote:

                              That's because a month does not have a constant value (i.e., 28, 29, 30, or 31 days) like the other items do.

                              I knew that too! :-D But it's ok, I guess what I was looking for doesn't exist (a tested/bug-free exact replica of DateAdd() in C++). Oh well :sigh:. Thanks anyway.

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

                              nm_114 wrote:

                              Well not really, as it doesn't handle months/years.

                              I already showed you how to add months to the SYSTEMTIME object. You would add years in the same fashion. What you have to account for when adding months is to make sure to increment years accordingly. For example, if it's September and you want to add four months, you would use:

                              wMonth = (wMonth + 4) % 12; // which would produce 0, for January

                              But you would then have to add one to the year.


                              "Approved Workmen Are Not Ashamed" - 2 Timothy 2:15

                              "Judge not by the eye but by the heart." - Native American Proverb

                              N 1 Reply Last reply
                              0
                              • D David Crow

                                nm_114 wrote:

                                Well not really, as it doesn't handle months/years.

                                I already showed you how to add months to the SYSTEMTIME object. You would add years in the same fashion. What you have to account for when adding months is to make sure to increment years accordingly. For example, if it's September and you want to add four months, you would use:

                                wMonth = (wMonth + 4) % 12; // which would produce 0, for January

                                But you would then have to add one to the year.


                                "Approved Workmen Are Not Ashamed" - 2 Timothy 2:15

                                "Judge not by the eye but by the heart." - Native American Proverb

                                N Offline
                                N Offline
                                nm_114
                                wrote on last edited by
                                #18

                                Yep. I guess I was hesitant to write my own because I don't know how to account for leap years, but I guess it's not critical. I'll probably use that other article and write something similar to your example for months/years. Thanks.

                                D 1 Reply Last reply
                                0
                                • N nm_114

                                  Yep. I guess I was hesitant to write my own because I don't know how to account for leap years, but I guess it's not critical. I'll probably use that other article and write something similar to your example for months/years. Thanks.

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

                                  nm_114 wrote:

                                  ...I don't know how to account for leap years...

                                  Are you kidding? Leap year algorithms/functions are plentiful here at CP, or even Googling for them.


                                  "Approved Workmen Are Not Ashamed" - 2 Timothy 2:15

                                  "Judge not by the eye but by the heart." - Native American Proverb

                                  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