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

2 Answers 2

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'
}
5
  • I try it but I get Invalid date into my field... format of my field is "dd/mm/yyyy"
    – Ledwing
    Oct 3, 2017 at 23:41
  • check the updated answer! Oct 3, 2017 at 23:50
  • I try it, but same result, check my update in my question
    – Ledwing
    Oct 4, 2017 at 0: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 4, 2017 at 5:07
  • Glad to hear that, I think These links would help you why it worked stackoverflow.com/questions/242627/… , stackoverflow.com/questions/4321270/… Oct 4, 2017 at 7: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

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.