I have JSON Data from my Appscript REST API, how can I convert the date to be a month in number eg if the date contains 01 I only obtain 1/or instead of "2020-01-15T22:00:00.000Z
Here is the data sample below:
{
"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
}
CodePudding user response:
You can use the Date() constructor to parse the dates since they are in ISO format, then use getUTCMonth() to get the month index. The month number will be one greater than this.
You can also use String.split()
to get the same value.
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; }
<iframe name="sif1" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>