答案 0 :(得分:2)
要以29 Apr, 2018
格式获取日期,可以使用两个选项:
您可以使用 momentjs .format()
method ,如下所示:
var dateStr = '2018-04-29';
var dateWithMoment = moment(dateStr, 'YYYY-MM-DD').format('DD MMM, YYYY');
您可以像这样使用toLocaleString()
method:
var options = {
year: "numeric",
month: "short",
day: "numeric"
};
console.log((new Date(dateStr)).toLocaleString("en-US", options));
演示:
这是演示这两种方法的演示:
var dateStr = '2018-04-29';
var dateWithMoment = moment(dateStr, 'YYYY-MM-DD').format('DD MMM, YYYY');
console.log(dateWithMoment);
var options = {
year: "numeric",
month: "short",
day: "numeric"
};
console.log((new Date(dateStr)).toLocaleString("en-US", options));
<script src="http://momentjs.com/downloads/moment.js"></script>
答案 1 :(得分:1)
var givenDate = '2018-04-29'
var resultDate = moment(givenDate, 'YYYY-MM-DD').format('DD MMM, YYYY')
答案 2 :(得分:0)
如果片刻不适合您,则可以使用香草js,如下所示:
const monthNames = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"];
// Date to convert
let myDate = "2018-04-29";
let components = myDate.split('-');
// 29 Apr, 2018
let newDate = components[2] + " " +monthNames[parseInt(components[1]) - 1] +", " +components[0];
console.log(newDate);
答案 3 :(得分:0)
Momentjs应该可以做您想做的事。 (请参阅Li357的答案)。
如果要寻找替代品,您可能想看看: