How do you display JavaScript datetime in 12 hour AM/PM format?

后端 未结 27 3351
慢半拍i
慢半拍i 2020-11-22 02:36

How do you display a JavaScript datetime object in the 12 hour format (AM/PM)?

27条回答
  •  野的像风
    2020-11-22 03:21

    If you just want to show the hours then..

    var time = new Date();
    console.log(
      time.toLocaleString('en-US', { hour: 'numeric', hour12: true })
    );  

    Output : 7 AM

    If you wish to show the minutes as well then...

    var time = new Date();
    console.log(
      time.toLocaleString('en-US', { hour: 'numeric', minute: 'numeric', hour12: true })
    );

    Output : 7:23 AM

提交回复
热议问题