locked
Convert a value obtained in Hours: Minutes: Second to Hours: Minutes RRS feed

  • Question

  • User1692641958 posted

    Hello, I currently have a value obtained that gives me in Hours: Minutes: Seconds what I want is to turn it into Hours: Minutes

    //Value Obtained by the id
     var HoraActuales = $('input:text[id=COMD_FECHA]').val();
     console.log(HoraActuales)//=> An example comes out 10:40:9 
    //What I want is to make it 10:40

    Friday, October 5, 2018 3:51 PM

All replies

  • User2103319870 posted

    RockChris

    I currently have a value obtained that gives me in Hours: Minutes: Seconds what I want is to turn it into Hours: Minutes

    If you are OK with using ThirdParty js file then you can take a look at Moment.js which has some useful methods to manipulate the date value.

    To achieve your requirement you can use below code

     var HoraActuales = $('input:text[id=COMD_FECHA]').val();
     console.log(moment(HoraActuales, 'HH:mm:ss').format('hh:mm'));
    Friday, October 5, 2018 5:35 PM
  • User-893317190 posted

    Hi RockChris,

    If you only want to get the hour and minute from  your textbox's value,since it is a string , you could use substring method to get part of the string.

    Below is my code.

     <asp:TextBox ID="TextBox1" runat="server" Text="10:40:9">
    
            </asp:TextBox>
            <input type="button"  value="click"/>
            <script>
                $(function () {
                  
                    $("input[type=button]").click(function () {
    
                        var value = $("input:text[id=TextBox1]").val();
                        if (value.indexOf(":") != value.lastIndexOf(":")) {  //ensure there are more than one ":"
                             $("input:text[id=TextBox1]").val(value.substring(0, value.lastIndexOf(":")));  // get the hour and minute part of time
                        }
                       
                    })
                })
            </script>

    The result.

    Best regards,

    Ackerly Xu

    Monday, October 8, 2018 6:31 AM