Convert a value obtained in Hours: Minutes: Second to Hours: Minutes

3

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

//Valor Obtenido por el id
 var HoraActuales = $('input:text[id=COMD_FECHA]').val();
 console.log(HoraActuales)//=> Sale un ejemplo 10:40:9 
 //Lo que quiero es hacer que sea 10:40
    
asked by Chris Alexis 05.10.2018 в 18:06
source

4 answers

5
//Intenta tomar el valor del input así:
var HoraActuales = $('input#COMD_FECHA').val();

//Lo que tienes que hacer es tomar los primeros caracteres que quieres mostrar
//Usa la función substring()
console.log(HoraActuales.substring(0,5));
    
answered by 05.10.2018 / 18:20
source
2

It's easy if you only work with html css and js you can add moment.js download if you are using nodejs you can install it with npm install moment --save # npm for more information about moment.js see the documentation

(function()
{
  // instantiate a moment object
  var NowMoment = moment().format('h:mm'); // October 5th 2018, 2:41:19 am
  
  // instantiate a JavaScript Date object
  var NowDate = new Date();
  
  // display value of moment object in #displayMoment div
  var eDisplayMoment = document.getElementById('displayMoment');
  eDisplayMoment.innerHTML = NowMoment;
  
  // display value of Date object in #displayJsDate div
  var eDisplayDate = document.getElementById('displayJsDate');
  eDisplayDate.innerHTML = NowDate;
})();
<script src="http://momentjs.com/downloads/moment.js"></script>
<h2>JavaScript Date</h2>
<!-- container for JavaScript Date output -->
<div id="displayJsDate"></div>

<h2>Moment Date</h2>
<!-- container for Moment.js output -->
<label>Formato:'h:mm'</label>
<div id="displayMoment"></div>
    
answered by 05.10.2018 в 20:59
0
       <div class="col-md-2">
                            <div class="form-group">
                                <label>Fecha de Atención</label>
                                <input type="text" class="form-control" id="COMD_FECHA_ATENCION" name="COMD_FECHA_ATENCION" value="@Model.COMD_FECHA_ATENCION.ToString("dd/MM/yyyy")" />
                            </div>
                        </div>
                         <div class="col-md-2">
                            <div class="form-group">
                                <label for="COMD_FECHA">Hora de Atención</label>
                                <input type="hidden" id="txtHoraEAtencion" value="@Model.COMD_FECHA.ToString("HH:mm")">
                                <input type="text" class="form-control" id="COMD_FECHA" name="COMD_FECHA" value="@Model.COMD_FECHA" />
                            </div>
                        </div>
    
answered by 05.10.2018 в 18:45
0

Try this, here I leave the code, I assume the variable you receive is string:

<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
</head>
<body>

<p>Click the button to extract characters from the string.</p>

<button onclick="myFunction()">Intentalo</button>

<p id="resuesta"></p>
<input type="text"  id="COMD_FECHA" name="COMD_FECHA" value='10:40:9' />

<script>
function myFunction() {
    var temp=$('#COMD_FECHA').val().toString().substr(0,5)
    document.getElementById("resuesta").innerHTML = temp;
}
</script>

</body>
</html>
    
answered by 08.10.2018 в 23:57