Reputation: 1350
I have time slots as 10:00:00, 12:00:00
etc.
I want to get current moment date object with these times.
I am trying this way.
console.log(moment('10:00:00', 'YYYY-MM-DD HH:ss:mm').toDate())
But it logs Invalid Date
.
Upvotes: 0
Views: 43
Reputation: 1242
This is the shortest way
// If you want set in your local
const localDate = moment('10:00:00', 'HH:mm:ss').toDate();
console.log('Local:', localDate);
// If you want set like utc
const utcDate = moment.utc('10:00:00', 'HH:mm:ss').toDate();
console.log('UTC:', utcDate)
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.29.1/moment.min.js"></script>
Upvotes: 2
Reputation: 1350
I solved this as below. I thought there would some method in the library to do it just in one line.
const today = moment().format('YYYY-MM-DD');
const time = '10:00:00';
console.log(moment(`${today} ${time}`).format('YYYY-MM-DD HH:mm:ss'));
Upvotes: 0
Reputation: 2493
This will help you.
var date1 = moment().set({ "hour": 10, "minute": 32 });
console.log(date1.toDate());
const date = "2017-03-13";
const time = "18:00";
const timeAndDate = moment(date + ' ' + time);
console.log(timeAndDate.toDate());
moment(moment().format('YYYY-MM-DD') + ' ' + time).toDate()
Upvotes: 0