close

How to set time with date in momentjs

Hello Guys, How are you all? Hope You all Are Fine. Today we are going to learn How to set time with date in momentjs in javascript. So Here I am Explain to you all the possible methods here.

Without wasting your time, Let’s start This Article.

How to set time with date in momentjs ?

  1. How to set time with date in momentjs ?

    to set time with date in momentjs 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. How to set time with date in momentjs ?

    to set time with date in momentjs 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

Method 1

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

Method 2

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

Summery

It’s all About this article. Hope all method helped you a lot. Comment below Your thoughts and your queries. Also, Comment below which method worked for you? Thank You.

Leave a Comment