最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - moment.js set time on existing date is not setting the time correctly - Stack Overflow

programmeradmin2浏览0评论
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,

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,

Share Improve this question asked Dec 3, 2015 at 15:43 activebizactivebiz 6,2309 gold badges43 silver badges65 bronze badges 1
  • 2 moment(end) makes a new Moment object with a new Date buried inside it. Changes to that won't affect the original Date instance. – Pointy Commented Dec 3, 2015 at 15:44
Add a comment  | 

1 Answer 1

Reset to default 16

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();
发布评论

评论列表(0)

  1. 暂无评论