0

I have a date column (Date Time only) Display format for the column is standard

I tried the below

var te = new Date(ctx.CurrentItem.Due_x0020_Date);
I get this output : Fri Mar 12 2021 00:00:00 GMT+1100 (Australian Eastern Daylight Time)

but the listview date column is showing 3/12/2021

The month is showing as date and not sure why.

Also when try deducting a year like this

te.setFullYear(te.getFullYear() - 1);
console.log(te.toLocaleDateString());

I get 3/12/2020 which is correct but with date like this 30/06/2022 i get error invalid date. Is it because the date has a double digit?

Thanks in advance

2
  • Anything to do with SharePoint regional settings? Aug 12 '21 at 8:24
  • Hi Jefin...regional setting is all good. The date on the list itself is in the correct format
    – naijacoder
    Aug 12 '21 at 11:37
0

Are you setting this date in "DD/MM/YYYY" format using javascript or does it appear automatically due to site regional settings?

You could extract date, month, and year separately and reduce the year value and make the date again. Below js code could help.

dateString = '30/06/2022';

function formatDate(dateString)
    {
        var allDate = dateString.split('/');
        var thisDate = allDate[0];
        var thisMonth = allDate[1];
        var thisYear = allDate[2];
        thisYear--;
        var newDate  = thisDate + "/" + thisMonth + "/" + thisYear;
        return newDate;
    }
1
  • Thanks Sajid will get that a shot but i actually wrote a similar function like below. Will try your function and see how i go function formatDt(dateObject) { var d = new Date(dateObject); var day = d.getDate(); var month = d.getMonth() + 1; var year = d.getFullYear() -1; if (day < 10) { day = "0" + day; } if (month < 10) { month = "0" + month; } var date = day + "/" + month + "/" + year; return date; };
    – naijacoder
    Aug 12 '21 at 22:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.