Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

moment.js set time on existing date is not setting the time correctly

let end: Date = new Date();
    let h: string = "13";
    let m: string = "20";
    moment(end).set({ hour: parseInt(h, 10), minute: parseInt(m, 10) });

I am trying to set time on an existing date. however the above doe is not setting the time in the date. The time always comes as ...00:00:...

Any idea?

Thanks,

like image 535
daehaai Avatar asked Dec 03 '15 15:12

daehaai


People also ask

How do you format time in a moment?

js parsing date and time. We can parse a string representation of date and time by passing the date and time format to the moment function. const moment = require('moment'); let day = "03/04/2008"; let parsed = moment(day, "DD/MM/YYYY"); console.

What timezone does MomentJs use?

By default, moment objects are created in the local time zone. Local time zone - it's a time zone which is set in a browser or on your node. js server.


1 Answers

You have to convert your moment to a Date, and assign that Date object to your end variable:

end = moment(end)
    .set({ hour: parseInt(h, 10), minute: parseInt(m, 10) })
    .toDate();
like image 108
mk. Avatar answered Oct 23 '22 12:10

mk.