javascript - 將月份從數字值改為三個字母
我正在從伺服器獲取 date time 的值到變數 a1:
let a1 = (new Date(estimatedServerTimeMs));console.log 的 a1 Sun Apr 05 2020 11:36:56 GMT+0530 (印度標準時間)
我把它拆分成像這樣的簡單日期格式 5-3-2020,但我希望它像 05-APR-2020 一樣
以下是我的程式碼:
let a1 = (new Date(estimatedServerTimeMs));
console.log(a1);
let show_year_month_date = a1.getDate()+'-'+ a1.getMonth() +'-'+ a1.getFullYear();
console.log(show_year_month_date);
我該如何:
- 將日期改為兩位數
- 將月份從數字改為三個字母的表示
答案#1
試試這個方法
let a1 = (new Date(estimatedServerTimeMs));
const dtf = new Intl.DateTimeFormat('en', { year: 'numeric', month: 'short', day: '2-digit' });
const [{ value: month },,{ value: day },,{ value: year }] = dtf.formatToParts(a1);
let show_year_month_date = day + '-' + month.toUpperCase() + '-' + year;
console.log(show_year_month_date);
