Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Howto add QTime to a QDateTime object?
Forum Update on Tuesday, May 27th 2025

Howto add QTime to a QDateTime object?

Scheduled Pinned Locked Moved Unsolved General and Desktop
6 Posts 3 Posters 3.0k Views 3 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.
  • C Offline
    C Offline
    cdwijs
    wrote on last edited by cdwijs
    #1

    Hi All,

    I have a QDateTime object, and I would like to add a QTime to it.

    How can I do this?

    kind regards,
    Cedric

    the_T 1 Reply Last reply
    0
    • C cdwijs

      Hi All,

      I have a QDateTime object, and I would like to add a QTime to it.

      How can I do this?

      kind regards,
      Cedric

      the_T Offline
      the_T Offline
      the_
      wrote on last edited by
      #2

      @cdwijs

      Do I understand correctly that you want to set the time of a QDateTime object from a QTime object?

         QDateTime::setTime(const QTime &time)
      

      allows you to set the time

      -- No support in PM --

      1 Reply Last reply
      1
      • C Offline
        C Offline
        cdwijs
        wrote on last edited by
        #3

        Hi The_,

        Almost. I have a QTimeEdit, and 2 QTimeDate Edits:
        QTimeEdit time;
        QTimeDate timedate1;
        QTimeDate timedate2;

        I would like to do this:
        timedate2 = timedate1 + time;

        Kind regards,
        Cedric

        the_T 1 Reply Last reply
        0
        • C cdwijs

          Hi The_,

          Almost. I have a QTimeEdit, and 2 QTimeDate Edits:
          QTimeEdit time;
          QTimeDate timedate1;
          QTimeDate timedate2;

          I would like to do this:
          timedate2 = timedate1 + time;

          Kind regards,
          Cedric

          the_T Offline
          the_T Offline
          the_
          wrote on last edited by
          #4

          @cdwijs

          Maybe there is an easier way but you can calculate the miliseconds since start of day from your QTime and add it to the QDateTime then.

          datetime2 = datetime.addMSecs(time.msecsSinceStartOfDay());
          

          -- No support in PM --

          kshegunovK 1 Reply Last reply
          0
          • C Offline
            C Offline
            cdwijs
            wrote on last edited by
            #5

            Thanks. It works when I perform it in a few intermediate steps, but I get an error when I do it in one big step:
            '((MainWindow*)this)->MainWindow::twilightDurationEdit->QDateTimeEdit::time' does not have class type
            datetime = dateTimeEdit->dateTime().addMSecs(twilightDurationEdit->time.fromMSecsSinceStartOfDay());
            ^
            This code works:
            QDateTime datetime;
            QTime time;

            datetime = dateTimeEdit->dateTime();
            datetime = datetime.addDays(twilightDaysSpin->value());
            time = twilightDurationEdit->time();
            msecs = time.msecsSinceStartOfDay();
            datetime = datetime.addMSecs(msecs);

            This code doesn't compile:
            datetime = dateTimeEdit->dateTime().addMSecs(twilightDurationEdit->time.fromMSecsSinceStartOfDay());

            Kind regards,
            Cedric

            1 Reply Last reply
            0
            • the_T the_

              @cdwijs

              Maybe there is an easier way but you can calculate the miliseconds since start of day from your QTime and add it to the QDateTime then.

              datetime2 = datetime.addMSecs(time.msecsSinceStartOfDay());
              
              kshegunovK Offline
              kshegunovK Offline
              kshegunov
              Moderators
              wrote on last edited by
              #6

              @the_, @cdwijs
              The problem is, QDateTime keeps both the date and the time of day, so is there a guarantee that QDateTime points to the same point in time from which QTime starts? I really doubt it.

              @cdwijs

              twilightDurationEdit->time.fromMSecsSinceStartOfDay()
              

              You are using a function (i.e. QDateTime::time) as a variable.
              The last should be:

              twilightDurationEdit->time().fromMSecsSinceStartOfDay()
              

              However, you consider the note my post starts with.

              Read and abide by the Qt Code of Conduct

              1 Reply Last reply
              0

              • Login

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • Users
              • Groups
              • Search
              • Get Qt Extensions
              • Unsolved