How to set time with date in momentjs
for today's date with a different time:
moment(12, "HH")
Since this pops up in google results, moment now has a set(unit, value)
function and you can achieve this by:
const hours = 15;
const minutes = 32;
var date = moment("1946-05-21").set("hour", hours).set("minute", minutes);
or as a combined function
var date = moment("1946-05-21").set({"hour": 15, "minute": 32});
Note: the set
function requires the value to be Integer type
Moment.js does not provide a way to set the time of an existing moment through a string. Why not just concatenate the two:
var date = "2017-03-13";
var time = "18:00";
var timeAndDate = moment(date + ' ' + time);
console.log(timeAndDate);
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.17.1/moment.min.js"></script>
Alternatively, you can use two Moment objects and use the getters and setters. Although a far more verbose option, it could be useful if you can't use concatenation:
let dateStr = '2017-03-13',
timeStr = '18:00',
date = moment(dateStr),
time = moment(timeStr, 'HH:mm');
date.set({
hour: time.get('hour'),
minute: time.get('minute'),
second: time.get('second')
});
console.log(date);
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.17.1/moment.min.js"></script>