Arockiaraj
Arockiaraj

Reputation: 353

Convert time to DateTime using moment js

I have time in HH:mm format like 13:15. I want to convert it into 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 wanted output with the current date and given time like 2015-21-05 13:56

Upvotes: 6

Views: 16260

Answers (1)

Satpal
Satpal

Reputation: 133403

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

Use

var t = "13:56"; 
var cdt = moment(t, 'HH:mm');
console.log(cdt.toDate());
console.log(cdt.format('YYYY-MM-DD HH:mm'));
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.10.3/moment.js"></script>

Upvotes: 18

Related Questions