How to display a Date object in a specific format using JavaScript?

后端 未结 7 1438
不思量自难忘°
不思量自难忘° 2020-12-03 21:21

I have a Date object and I\'d like to display it in the below format:

var myDate = getDate();
// this format: \"13 Jan 2012 11:00am\";

How

相关标签:
7条回答
  • 2020-12-03 22:16

    Use toLocaleString()

    const date = new Date();
    
    const formattedDate = date.toLocaleString("en-GB", {
      day: "numeric",
      month: "short",
      year: "numeric",
      hour: "numeric",
      minute: "2-digit"
    });
    
    console.log(formattedDate); // '18 Jan 2020, 18:20'

    0 讨论(0)
提交回复
热议问题