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

后端 未结 27 3073
慢半拍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:22

    use dateObj.toLocaleString([locales[, options]])

    Option 1 - Using locales

    var date = new Date();
    console.log(date.toLocaleString('en-US'));
    

    Option 2 - Using options

    var options = { hour12: true };
    console.log(date.toLocaleString('en-GB', options));
    

    Note: supported on all browsers but safari atm

提交回复
热议问题