Convert time to DateTime using moment js

I have a time formatted HH:mm

for example 13:15

. I want to convert it to YYYY-MM-DD HH:mm

using moment.js.

I tried the following code.

var t = "13:56"; 
var cdt = moment(t).format('YYYY-MM-DD HH:mm');
alert(cdt);

      

I need an output with the current date and the specified time, for example 2015-21-05 13:56

+3


source to share


1 answer


You need to use the moment constructor string-format to pass the string and the format that the input string is in.

Using



var t = "13:56"; 
var cdt = moment(t, 'HH:mm');
alert(cdt.toDate());
alert(cdt.format('YYYY-MM-DD HH:mm'))
      

<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.10.3/moment.js"></script>
      

Run codeHide result


+11


source







All Articles