1

I use this code to compare two dates, the date of today and the date in the list item. On the screen the dates look fine but when I check them in the debugger the dates are way off. Some are even one year ahead. Why does the code return a different date than what I see on the screen?

<script type="text/javascript">
SP.SOD.executeFunc("clienttemplates.js", "SPClientTemplates", function() { 
    SPClientTemplates.TemplateManager.RegisterTemplateOverrides({
        OnPostRender: function (ctx) {
            // get today's date
            var today = new Date();
            var rows = ctx.ListData.Row;
            for (var i = 0; i < rows.length; i++) {
                // get the date set in your date YourDateField
                var itemDate = new Date(rows[i]['DueDate']);
                var rowId = GenerateIIDForListItem(ctx, rows[i]);
                var row = document.getElementById(rowId);
        console.log("itemdate: " + itemDate);
                if (row!=null&&itemDate <= today) {                  
                    row.style.backgroundColor = '#FF0000';                  
                }
            }
        }
    });
});
</script>

Does anyone have a clue?

1 Answer 1

0

This is a known behavior in SharePoint. SharePoint stores all its date/time fields in UTC time.

So, when you try to access the date values using JSOM or REST endpoints that returns the date string in a format that is specific to the regional settings of the person asking for them.

The causes and solutions for this issue are explained in detail in below article:

SharePoint time, is not your time, is not their time

Additional references:

  1. SharePoint JSOM datetime values in different timezones
4
  • Ganesh, I checked the link you provided but it only tells about time differences in case of local settings. However in my case I get dates that are like somewhere in 2021 or 2022. Even the listview shows the correct dates. Aug 4, 2020 at 14:24
  • Are you saying that if you enter dates from year 2020 in date-time column then it returns dates from 2021 or 2022 in your OnPostRender function? Aug 4, 2020 at 14:30
  • Yes that's exactly what's happening. Aug 4, 2020 at 14:37
  • 21-8-2020 08:30 ----> Wed Sep 8 08:30:00 UTC+0200 2021 24-8-2020 08:00 ----> Wed Dec 8 08:00:00 UTC+0100 2021 31-7-2020 08:00 ----> Thu Jul 7 08:00:00 UTC+0200 2022 The first date is the correct one that I see in the list and the second is what I get back when I want to compare them using javascript Aug 4, 2020 at 14:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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