Jack
Jack

Reputation: 329

get today's date with parsed time in moment

I am trying to get the today's date with the time which I specified using moment js. My attempt is as below.

const time = '18:00'
const timeAndDate = moment(time)

But when I print timeAndDate it says invalid date. Is there any other way to achieve this?

Upvotes: 0

Views: 59

Answers (2)

AlTheLazyMonkey
AlTheLazyMonkey

Reputation: 1242

It's enough to specify the time format like second parameter in moment

const timeOfToday = moment('18:00', 'HH:mm');
console.log(timeOfToday);
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.29.1/moment.min.js"></script>

Upvotes: 1

const minute = 0
const hour = 18
const timeAndDate = moment().hours(hour).minutes(minute);

https://momentjs.com/docs/#/get-set/hour/

Upvotes: 1

Related Questions