1

I have this query running from JSLink into list. It get current time from moment.js and compare date with sharepoint column ctx.CurrentItem.Cumplimiento; and return result into column sCumplimientoInicial

 function overrideCumplimiento(ctx) {
         var sIconFileName = "";


    var currentServerDateTime = moment().format("DD/MM/YYYY"); 
    var sCumplimiento = ctx.CurrentItem.Cumplimiento;
    var sCumplimientoInicial = ctx.CurrentItem.Fecha_x0020_Compromiso_x0020_Ini;


      if(currentServerDateTime < sCumplimientoInicial){
       sCumplimiento  = 'Yes'
      }else{
       sCumplimiento  = 'No'
      }
    return sCumplimiento;
   }

Problem is when currentServerTime is bigger than sCumplimientoInicial it return "Yes" instead "No", and it always return Yes. Any idea what is wrong there?

I think it is because sCumplimiento is an string and no date. Because if I select first, second of any month it return no so it is not comparing from dates just from string. How can I achieve this comparation of dates correctly?

UPDATE: As comments below I try to do it using

 var sCumplimientoInicial = moment("ctx.CurrentItem.Fecha_x0020_Compromiso_x0020_Ini");

But I getting "Invalid Date"

enter image description here

Ledwing
  • 465
  • 4
  • 17

2 Answers2

1

To cast a string field to date try this

var FDate = new Date(your field with DateTime Data Type);

Ex: var FDate = new Date(ctx.CurrentItem.Fecha_x0020_Compromiso_x0020_Ini);

Note: in case your field is string, try to parse your code with moment

var momentDate = moment("your field");

In case it's not working, try to customize your string in the correct date format "MM/dd/yyyy" then parse the output to date as the following

var day  = str.substring(0,2);
var mon = str.substring(3,5);
var yr  = str.substring(6,10);  
var dateformated = mon + "/" + day + "/" + yr;
var sCumplimientoInicial = Date.parse(dateformated);

To compare with today try this

 var today = new Date();

So the condition should look like

if(today < FDate )

{
       sCumplimiento  = 'Yes'

}
else
{
       sCumplimiento  = 'No'
}
Mohamed El-Qassas MVP
  • 45,382
  • 9
  • 53
  • 96
  • I try it but I get Invalid date into my field... format of my field is "dd/mm/yyyy" – Ledwing Oct 03 '17 at 23:41
  • check the updated answer! – Mohamed El-Qassas MVP Oct 03 '17 at 23:50
  • I try it, but same result, check my update in my question – Ledwing Oct 04 '17 at 00:05
  • Date.parse work but I don´t know why it works because their output are numbers not formated date, so I´m comparing a date formated as "dd/mm/yyyy" with parse date like 1509001200000, can you explain how it works? – Ledwing Oct 04 '17 at 05:07
  • Glad to hear that, I think These links would help you why it worked https://stackoverflow.com/questions/242627/why-does-date-parse-not-return-a-date-object , https://stackoverflow.com/questions/4321270/regarding-javascript-new-date-and-date-parse – Mohamed El-Qassas MVP Oct 04 '17 at 07:26
0

You do not have to do the calculations;

SharePoints adds a 'GetDaysAfterToday' function

see: Change the color of a row when Date >= today's date

Danny '365CSI' Engelman
  • 21,176
  • 7
  • 35
  • 79