Does momentjs provide any option to set time with particular time ?
var date = "2017-03-13";
var time = "18:00";
var timeAndDate = moment(date).startOf(time);
console.log(timeAndDate);
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.17.1/moment.min.js"></script>
When you have separated string for date and time you can parse first as date and second as duration and just add them. This should create
moment
with proper date and timeJust incase anyone is wondering how to set the time on a Date Object, here's how I did it:
Since this pops up in google results, moment now has a
set(unit, value)
function and you can achieve this by:or as a combined function
Note: the
set
function requires the value to be Integer typeMoment.js does not provide a way to set the time of an existing moment through a string. Why not just concatenate the two:
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: