Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. new Date() in Qml Js function return a TypeError

new Date() in Qml Js function return a TypeError

Scheduled Pinned Locked Moved Solved QML and Qt Quick
9 Posts 4 Posters 1.2k Views
  • 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.
  • D Offline
    D Offline
    DavidM29
    wrote on last edited by
    #1

    Hello,

    I do have implement a function to get the date and time every second but once I reach the new Date() call I get a TypeError.

    Here is the code I run :

        Timer {
            id: timer
            interval: 1000
            repeat: true
            running: true
    
            onTriggered:
            {
               getDate()
    
            }
         }
    
        function getDate(){  
            var today = new Date() //Here is the line throwing the error
            var date = Qt.formatDate(today, "dd/MM/yyyy")
            var dateTime = date
    
            labelOptionDate = dateTime
    
        }
    

    Here is the error :

    qrc:/OptionDate.qml:84: TypeError: Type error
    
    Pradeep P NP 1 Reply Last reply
    1
    • IntruderExcluderI Offline
      IntruderExcluderI Offline
      IntruderExcluder
      wrote on last edited by
      #2

      Provided example working fine for me:

          Timer {
              id: timer
              interval: 1000
              repeat: true
              running: true
      
              onTriggered: getDate()
          }
      
          function getDate() {
              console.log("Requesting date");
              var date = new Date();
          }
      

      I can see call of the function every second. Qt 5.12.3.

      1 Reply Last reply
      1
      • D DavidM29

        Hello,

        I do have implement a function to get the date and time every second but once I reach the new Date() call I get a TypeError.

        Here is the code I run :

            Timer {
                id: timer
                interval: 1000
                repeat: true
                running: true
        
                onTriggered:
                {
                   getDate()
        
                }
             }
        
            function getDate(){  
                var today = new Date() //Here is the line throwing the error
                var date = Qt.formatDate(today, "dd/MM/yyyy")
                var dateTime = date
        
                labelOptionDate = dateTime
        
            }
        

        Here is the error :

        qrc:/OptionDate.qml:84: TypeError: Type error
        
        Pradeep P NP Offline
        Pradeep P NP Offline
        Pradeep P N
        wrote on last edited by Pradeep P N
        #3

        @DavidM29 said in new Date() in Qml Js function return a TypeError:

        labelOptionDate

        Can you tell me what is labelOptionDate in your code ?
        And from where you are accessing it ?

        Pradeep Nimbalkar.
        Upvote the answer(s) that helped you to solve the issue...
        Keep code clean.

        D 1 Reply Last reply
        3
        • Pradeep P NP Pradeep P N

          @DavidM29 said in new Date() in Qml Js function return a TypeError:

          labelOptionDate

          Can you tell me what is labelOptionDate in your code ?
          And from where you are accessing it ?

          D Offline
          D Offline
          DavidM29
          wrote on last edited by
          #4

          @Pradeep-P-N

          labelOptionDate is a Text component in my .qml file that I update with the value but I don't reach it at this point. I'm having an error before that.

          ODБOïO 1 Reply Last reply
          0
          • D DavidM29

            @Pradeep-P-N

            labelOptionDate is a Text component in my .qml file that I update with the value but I don't reach it at this point. I'm having an error before that.

            ODБOïO Offline
            ODБOïO Offline
            ODБOï
            wrote on last edited by
            #5

            hi
            @DavidM29 said in new Date() in Qml Js function return a TypeError:

            labelOptionDate is a Text component in my .qml

            then there is a type error here

            labelOptionDate = dateTime
            

            should be

            labelOptionDate.text = dateTime
            

            i often do this mistake

            D 1 Reply Last reply
            5
            • ODБOïO ODБOï

              hi
              @DavidM29 said in new Date() in Qml Js function return a TypeError:

              labelOptionDate is a Text component in my .qml

              then there is a type error here

              labelOptionDate = dateTime
              

              should be

              labelOptionDate.text = dateTime
              

              i often do this mistake

              D Offline
              D Offline
              DavidM29
              wrote on last edited by
              #6

              @LeLev
              Sorry I did not explain properly.
              It is a property that I use to set the Text of a Text component...
              The code I'm running used to work on another computer and still running on another one...

              property string labelOptionDate: getDate()
              
              Text {
                                  id: textOption
                                  text: labelOptionDate // Here is my labelOptionDate use
                                  color: Style.textColor
                                  font.family: localFont.name
                                  font.pixelSize: Style.globalTextSize
                                  anchors.verticalCenter: parent.verticalCenter
                              }
              
              ODБOïO 1 Reply Last reply
              0
              • D DavidM29

                @LeLev
                Sorry I did not explain properly.
                It is a property that I use to set the Text of a Text component...
                The code I'm running used to work on another computer and still running on another one...

                property string labelOptionDate: getDate()
                
                Text {
                                    id: textOption
                                    text: labelOptionDate // Here is my labelOptionDate use
                                    color: Style.textColor
                                    font.family: localFont.name
                                    font.pixelSize: Style.globalTextSize
                                    anchors.verticalCenter: parent.verticalCenter
                                }
                
                ODБOïO Offline
                ODБOïO Offline
                ODБOï
                wrote on last edited by ODБOï
                #7

                ok
                @DavidM29 said in new Date() in Qml Js function return a TypeError:

                property string labelOptionDate: getDate()

                this means labelOptionDate is = to the value returned by getDate(), but it returns nothing

                try

                property string labelOptionDate: "" // don't call the function here, let this empty , the function called by the timer will update this every second 
                
                D 1 Reply Last reply
                3
                • ODБOïO ODБOï

                  ok
                  @DavidM29 said in new Date() in Qml Js function return a TypeError:

                  property string labelOptionDate: getDate()

                  this means labelOptionDate is = to the value returned by getDate(), but it returns nothing

                  try

                  property string labelOptionDate: "" // don't call the function here, let this empty , the function called by the timer will update this every second 
                  
                  D Offline
                  D Offline
                  DavidM29
                  wrote on last edited by
                  #8

                  @LeLev
                  Sorry for the delay...
                  I don't know why but the day after the problem was gone... Maybe my clean and rebuild was not completely made...

                  But anyways @LeLev I took your remarks in consideration and changed my code in that way.

                  Thank you for the help

                  1 Reply Last reply
                  2
                  • ODБOïO Offline
                    ODБOïO Offline
                    ODБOï
                    wrote on last edited by
                    #9

                    @DavidM29
                    Perfect!
                    Make sure you mark this as SOLVED topic

                    1 Reply Last reply
                    1

                    • Login

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