Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Mobile and Embedded
  4. How to add a character to the beginning of a string?
Forum Updated to NodeBB v4.3 + New Features

How to add a character to the beginning of a string?

Scheduled Pinned Locked Moved Solved Mobile and Embedded
5 Posts 3 Posters 1.1k 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.
  • N Offline
    N Offline
    NTMS
    wrote on last edited by
    #1

    Hi,

    I parse json and I want to add "0" character to the beginning of the time string.
    The if(hourLenght === 1){ mHour = "0" + mHour } part is not working in my QML project.

    The whole code:

                if (request.readyState === XMLHttpRequest.DONE) {
                    if (request.status && request.status === 200) {
                        var result = JSON.parse(request.responseText)
                        // BUY
                        textSTG_BUY.text = result.currency.sterling.buy
     
                        // SELL
                        textSTG_SELL.text = result.currency.sterling.sell
    
                        // DATE
                        var temDate = result.tarih
                        var mYear = temDate.slice(0, 4);
                        var mMonth = temDate.slice(4, 6);
                        var mDay = temDate.slice(6, 8);
                        textDATE.text = mDay + "." + mMonth + "." + mYear
    
                        // TIME
                        var d = new Date(); // for now
                        var mHour = d.getHours();
                        var mMinutes = d.getMinutes();
    
                        **var hourLenght = mHour.lenght
                        if(hourLenght === 1){
                            mHour = "0" + mHour
                        }
    
                        var minuteLenght = mMinutes.lenght
                        if(minuteLenght === 1){
                            mMinutes = "0" + mMinutes
                        }**
    
                        textTIME.text = mHour + ":" + mMinutes
                    }
                    else {
                        //console.log("Log:", request.status, request.statusText)
                    }
                }
    
    JonBJ 1 Reply Last reply
    0
    • T Offline
      T Offline
      Tirupathi Korla
      wrote on last edited by
      #2

      Hi
      Try using formatTime instead of prepending "0" before hours/mins.

                  var time = new Date();
                  label.text = Qt.formatTime(time,"HH:mm")
      

      -Tirupathi

      1 Reply Last reply
      2
      • N NTMS

        Hi,

        I parse json and I want to add "0" character to the beginning of the time string.
        The if(hourLenght === 1){ mHour = "0" + mHour } part is not working in my QML project.

        The whole code:

                    if (request.readyState === XMLHttpRequest.DONE) {
                        if (request.status && request.status === 200) {
                            var result = JSON.parse(request.responseText)
                            // BUY
                            textSTG_BUY.text = result.currency.sterling.buy
         
                            // SELL
                            textSTG_SELL.text = result.currency.sterling.sell
        
                            // DATE
                            var temDate = result.tarih
                            var mYear = temDate.slice(0, 4);
                            var mMonth = temDate.slice(4, 6);
                            var mDay = temDate.slice(6, 8);
                            textDATE.text = mDay + "." + mMonth + "." + mYear
        
                            // TIME
                            var d = new Date(); // for now
                            var mHour = d.getHours();
                            var mMinutes = d.getMinutes();
        
                            **var hourLenght = mHour.lenght
                            if(hourLenght === 1){
                                mHour = "0" + mHour
                            }
        
                            var minuteLenght = mMinutes.lenght
                            if(minuteLenght === 1){
                                mMinutes = "0" + mMinutes
                            }**
        
                            textTIME.text = mHour + ":" + mMinutes
                        }
                        else {
                            //console.log("Log:", request.status, request.statusText)
                        }
                    }
        
        JonBJ Offline
        JonBJ Offline
        JonB
        wrote on last edited by JonB
        #3

        @NTMS
        @Tirupathi-Korla response above is the right way to do what you want.

        However, for the record and for your future, what is wrong with your code is the spelling in mHour.lenght & mMinutes.lenght. The correct spelling is length. (JavaScript being JavaScript, it allows you to mistype this "property" name without warning, and returns undefined as its value.) I suggest you also rename your variables in this light.

        1 Reply Last reply
        2
        • N Offline
          N Offline
          NTMS
          wrote on last edited by
          #4

          @Tirupathi-Korla and @JNBarchan I will check it tonight

          1 Reply Last reply
          0
          • N Offline
            N Offline
            NTMS
            wrote on last edited by
            #5

            @Tirupathi-Korla Thank you is working. @JNBarchan in my code I spell correctly but didn't work.

            Thank you.

            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