How to get current time in a format hh:mm AM/PM in Javascript?

后端 未结 4 1379
小鲜肉
小鲜肉 2021-01-05 11:26

I have a Javascript in which I need to paste the current time in a format HH:MM AM/PM. There\'s one catch - I need to put the time that starts in two hours from now, so for

4条回答
  •  醉酒成梦
    2021-01-05 12:23

    Simply, you can do this

    
    const date = new Date()
    const options = {
      hour: 'numeric',
      minute: 'numeric',
      hour12: true
    };
    const time = new Intl.DateTimeFormat('en-US', options).format(date)
    console.log(time)
    
    

    For more details, you can refer to the MDN docs regarding the same.

提交回复
热议问题