答案 0 :(得分:3)
您可以考虑即时格式化。 Momentjs是一个非常好的图书馆,提供日期格式 momentjs
检查此代码段
console.log(moment().format('LLLLL')); // J
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.17.1/moment.min.js"></script>
答案 1 :(得分:2)
一种可能的实施
Date
构造函数创建新日期。在这种情况下,我们将分别传递月,月,日,小时,分钟和秒。这里有几点要记住。月份从0开始而不是1,所以要获得一年中的第三个月(3月),你必须传入2.小时范围从0到23,所以我们需要传入17而不是5。test
功能的正则表达式。根据日期的字符串实现,我们将返回不同的后缀。${}
中并将字符串放在反引号中。
var months = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"];
var days = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"];
// Create a new date object and variables based upon this object
var date = new Date(2012,2,13,17,8,57);
var year = date.getFullYear();
var month = months[date.getMonth()];
var day = date.getDate();
var dayName = days[date.getDay()];
var hour = date.getHours();
var minute = date.getMinutes();
var second = date.getSeconds();
// Control formatting of time
var timePeriod = hour > 12 ? "PM" : "AM";
hour = hour < 10 ? "0" + hour : hour > 12 ? ((hour % 12) < 10 ? "0" + hour % 12 : hour % 12) : hour;
minute = minute < 10 ? "0" + minute : minute;
second = second < 10 ? "0" + second : second;
// Return the suffix of the day
day = /[4567890]$|11|12|13/.test(day) ? day + "th" :
/^1$|21|31/.test(day) ? day + "st" :
/^2$|22/.test(day) ? day + "nd" : day + "rd";
// Log the results
console.log(`${dayName} ${month} ${day}, ${year}, ${hour}:${minute}:${second} ${timePeriod}`);