如果 DateString 值為“ 19780000 ”。然后通過moment .js在UI頁面中只顯示像“1978”這樣的年份
如果 DateString 值為“ 19781100 ”。然后通過moment .js在UI頁面中顯示像“1978年11月”這樣的月份和年份
如果MOMENT .js 沒有實作,那么javascript怎么做。
請幫助我。提前致謝
uj5u.com熱心網友回復:
這是一個生成字串的簡單函式。它假定字串的年份部分是有效的。
function dateStringToUI(ds) {
const months = [null, "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"];
let uiString = "";
if (ds.length == 8) {
let yearPart = ds.substr(0, 4);
let monthPart = months[parseInt(ds.substr(4, 2))];
uiString = (monthPart !== null) ? `${monthPart} ${yearPart}` : yearPart;
}
return uiString;
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/531065.html
