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. add the specified number of days to current date

add the specified number of days to current date

Scheduled Pinned Locked Moved C / C++ / MFC
helpquestionc++
4 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.
  • Z Offline
    Z Offline
    zon_cpp
    wrote on last edited by
    #1

    Hi! i use localtime function to get current time, in "tm struct". how do i get a new "tm struct" that adds the specified number of days to the value of this instance. this is similar with DateTime::AddDays, but i don't use any MFC Class.

    time_t t;
    struct tm * tt;
    char buffer[50];
    time(&t);
    tt = localtime(&t);
    int dayOfWeek = tt->tm_wday; // days since sunday
    tt->tm_mday -= dayOfWeek;

    in this code there is a bug. if tt->tm_mday < dayOfWeek then ??? please help me.

    Zo.Naderi-Iran

    CPalliniC 1 Reply Last reply
    0
    • Z zon_cpp

      Hi! i use localtime function to get current time, in "tm struct". how do i get a new "tm struct" that adds the specified number of days to the value of this instance. this is similar with DateTime::AddDays, but i don't use any MFC Class.

      time_t t;
      struct tm * tt;
      char buffer[50];
      time(&t);
      tt = localtime(&t);
      int dayOfWeek = tt->tm_wday; // days since sunday
      tt->tm_mday -= dayOfWeek;

      in this code there is a bug. if tt->tm_mday < dayOfWeek then ??? please help me.

      Zo.Naderi-Iran

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

      Why don't you add the proper number of seconds (1 day is 86400 secs) directly to the time_t variable? :)

      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?

      Z 1 Reply Last reply
      0
      • CPalliniC CPallini

        Why don't you add the proper number of seconds (1 day is 86400 secs) directly to the time_t variable? :)

        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]

        Z Offline
        Z Offline
        zon_cpp
        wrote on last edited by
        #3

        oh, ok. thanks a lot, dear CPallini.

        Zo.Naderi-Iran

        R 1 Reply Last reply
        0
        • Z zon_cpp

          oh, ok. thanks a lot, dear CPallini.

          Zo.Naderi-Iran

          R Offline
          R Offline
          rahul kulshreshtha
          wrote on last edited by
          #4

          Another answer is To get system date: COleDateTime curDateTime; curDateTime = COleDateTime::GetCurrentTime(); COleDateTimeSpan represents a relative time, a time span. COleDateTimeSpan daySpan(25,0,0,0); 25 represents 25 days span. Next three zeros – hours, minutes, and seconds. Wanna get the date, 25 days from current date, add daySpan with curDateTime: COleDateTime comDateTime; comDateTime = curDateTime + daySpan; Also see CTime and CTimeSpan

          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