Rest API to get different data
-
08-02-2021 - |
Question
I have the following code to get values of a column.
function GetKPIS(EstruturaKPI){
jQuery.ajax({
url: "SITE/_api/web/lists/GetByTitle('KPI')/items?$filter=Title eq '" + EstruturaKPI + "'",
method: "GET",
headers: {
"Accept": "application/json; odata=verbose"
},
success: function (data) {
jQuery.each(data.d.results, function(key, value){
var UltimaData = (value.Data);
})
},
error: function (error) {
console.log(error);
}
});
With this code, I am able to get the DATE the user has input into the field. The problem is, the date is shown in YYYY-MM-DDZ(time), and I wanted the date to show DD-MM-YYYY ONLY (Without the time).
How can I achieve this?
Thank you so much
Solution
You can use the below code or use third-party js like moment to format dates.
function GetKPIS(EstruturaKPI){
jQuery.ajax({
url: "SITE/_api/web/lists/GetByTitle('KPI')/items?$filter=Title eq '" + EstruturaKPI + "'",
method: "GET",
headers: {
"Accept": "application/json; odata=verbose"
},
success: function (data) {
jQuery.each(data.d.results, function(key, value){
var date = new Date(value.Data); //assuming this is your date field
var day = date.getDate() < 10 ? '0' + date.getDate() : date.getDate();
var month = date.getMonth() < 10 ? '0' + date.getMonth() : date.getMonth();
var year = date.getFullYear();
var UltimaData = day + "-" + month + "-" + year;
})
},
error: function (error) {
console.log(error);
}
});
}
Licensed under: CC-BY-SA with attribution
Not affiliated with sharepoint.stackexchange