93

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>

enter image description here

7
  • 1
    moment(date+' '+time)?
    – BenM
    Feb 28, 2017 at 17:55
  • 2
    It's probably a good time to read the docs on parsing
    – chazsolo
    Feb 28, 2017 at 17:57
  • @BenM not working
    – Mo.
    Feb 28, 2017 at 18:02
  • @chazsolo I will have a look
    – Mo.
    Feb 28, 2017 at 18:02
  • @MuhammedAthimannil It is working, please see my answer; and importantly, the working snippet.
    – BenM
    Feb 28, 2017 at 18:03

5 Answers 5

132

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>

1
  • 3
    Concatenating is not a viable option if working with different time zones.
    – Eggon
    May 18, 2021 at 15:05
88

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

0
11

for today's date with a different time:

moment(12, "HH")
6

Just incase anyone is wondering how to set the time on a Date Object, here's how I did it:

const dateObj = new Date();

const dateStr = dateObj.toISOString().split('T').shift();
const timeStr = '03:45';

const timeAndDate = moment(dateStr + ' ' + timeStr).toDate();
1
  • Really you should not be altering datetimes using string operations. That's what a library like moment is for. Better to use moment(dateObj).hours(3).minutes(45)
    – Luke H
    Mar 22, 2022 at 3:01
5
var timeAndDate = moment(date).add(moment.duration(time))

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 time

2
  • Please edit your answer and add some context by explaining how your answer solves the problem, instead of posting code-only answer. From Review Dec 30, 2019 at 16:39
  • Does not give the accurate results.
    – Gezim
    Jan 14, 2021 at 3:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.