我有來自我的 Appscript REST API 的 JSON 資料,如何將日期轉換為月份數字,例如,如果日期包含 01,我只獲得 1/或而不是“2020-01-15T22:00:00.000Z
下面是資料示例:
{
"Employee Name":"Ntombi Hlatshwayo",
"Issueing Manager":"Nomsa Nxumalo",
"Type Of A Warning":"Insubordination Final",
"Date":"2020-09-15T22:00:00.000Z",
"Current Date":"2021-11-02T12:24:32.428Z",
"Status":413
}
uj5u.com熱心網友回復:
您可以使用 Date() 建構式來決議日期,因為它們是 ISO 格式,然后使用 getUTCMonth() 來獲取月份索引。月份數將比這大一。
您也可以使用String.split()來獲得相同的值。
let o = {
"Employee Name":"Ntombi Hlatshwayo",
"Issueing Manager":"Nomsa Nxumalo",
"Type Of A Warning":"Insubordination Final",
"Date":"2020-09-15T22:00:00.000Z",
"Current Date":"2021-11-02T12:24:32.428Z",
"Status":413
}
function getMonthFromISODate(dt) {
return new Date(dt).getUTCMonth() 1;
}
console.log('Using Date()');
for (let dt of [o.Date, o['Current Date']]) {
console.log(`Date: ${dt}, Month: ${getMonthFromISODate(dt)}`);
}
function getMonthFromISODateII(dt) {
return dt.split('-')[1];
}
console.log('Using String.split()');
for (let dt of [o.Date, o['Current Date']]) {
console.log(`Date: ${dt}, Month: ${getMonthFromISODateII(dt)}`);
}
.as-console-wrapper { max-height: 100% !important; top: 0; }
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/345106.html
標籤:javascript json 目的
