如何使用JavaScript以特定格式显示Date对象



我有一个Date对象,我想以以下格式显示它:

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

这怎么可能呢?

谢谢,

使用内置的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'

编辑:对于现代应用程序(不支持IE),请查看@zendka的答案https://stackoverflow.com/a/59802446/483616


有一个很棒的JavaScript库可以很好地处理这个问题,并且只缩小了5.5kb。

http://momentjs.com/

它看起来像这样:

moment().format('MMMM Do YYYY, h:mm:ss a'); // February 25th 2013, 9:54:04 am
moment().subtract('days', 6).calendar(); // "last Tuesday at 9:53 AM"

您也可以将日期作为具有格式的StringDate对象传入。

var date = new Date();
moment(date); // same as calling moment() with no args
// Passing in a string date
moment("12-25-1995", "MM-DD-YYYY");

还支持除英语以外的其他语言,如俄语、日语、阿拉伯语、西班牙语和其他语言。

查看文档。

如果您不想使用任何库:

<script type="text/javascript">
  var myDate = new Date();
  var month=new Array();
  month[0]="Jan";
  month[1]="Feb";
  month[2]="Mar";
  month[3]="Apr";
  month[4]="May";
  month[5]="Jun";
  month[6]="Jul";
  month[7]="Aug";
  month[8]="Sep";
  month[9]="Oct";
  month[10]="Nov";
  month[11]="Dec";
  var hours = myDate.getHours();
  var minutes = myDate.getMinutes();
  var ampm = hours >= 12 ? 'pm' : 'am';
  hours = hours % 12;
  hours = hours ? hours : 12;
  minutes = minutes < 10 ? '0'+minutes : minutes;
  var strTime = hours + ':' + minutes + ampm;
  // e.g. "13 Nov 2016 11:00pm";
  alert(myDate.getDate()+" "+month[myDate.getMonth()]+" "+myDate.getFullYear()+" "+strTime);
</script>

javascript有很多日期格式化包,我在Steven Levitan的日期格式方面取得了巨大成功。

dateFormat(getDate(), "dd mmm yyyy hh:MMtt");

编辑:如果您喜欢这种风格,它还会在Date.prototype中添加format方法:

getDate().format("dd mmm yyyy hh:MMtt");

现在可以用来格式化日期的另一个JavaScript内置库是Intl库。

// use formatToParts() to convert a date and put it into an array for the different parts of the date
var customDateArray = new Intl.DateTimeFormat('en-US', { day: 'numeric', month: 'short', year: 'numeric', hour: 'numeric', minute: 'numeric' }). formatToParts(new Date()),
  dateParts = {}
// This converts the array to an object to make it easier to pick out what you want using template literals.
customDateArray.map(({type, value}) => {
  dateParts[type] = value
})
console.log(`${dateParts.day} ${dateParts.month} ${dateParts.year} ${dateParts.hour}:${dateParts.minute}${dateParts.dayPeriod.toLowerCase()}`)

更多信息:https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/DateTimeFormat

您可以使用JavaScript中的date对象显示不同的日期格式。不同的日期形式可能是

  1. 仅年份(YYYY)
  2. 年和月(YYYY-MM)
  3. 完成日期(YYYY-MM-DD)
  4. 包含日期名称的完整日期(YYYY-MM-DD DayName)
  5. 不同时间格式的日期

使用以下JavaScript代码以仅年份格式显示日期。

function displayDate(){
var now = new Date();
var year=now.getFullYear();
date.innerHTML=year
}
window.onload=displayDate;
<div id="date"></div>

这篇文章将更有助于了解更多关于显示其他更多日期格式的信息:https://www.siteforinfotech.com/2014/03/how-to-display-date-format-in-javascript.html

最新更新