Jump to content
  • 0

How to pick the earliest of several dates?


BjornSkifs

Question

9 answers to this question

Recommended Posts

  • 0

Hi Bjorn,

You can have a java script on your page to get both dates and return the earlier one in another field:

function whichdate()

{

var firstDate=new Date("[@field:FirstDateFieldName*]");

var SecondDate=new Date("[@field:SecondDateFieldName*]");

if (firstDate > SecondDate)

{

document.getElementById("Id of the field you would like to show this date on").value = SecondDate;

}

else

{

document.getElementById("Id of the field you would like to show this date on").value = firstDate;

}

}

whichdate();

You need to replace all IDs in the script with valid field IDs.

Cheers :!: :!: :!:

Link to comment
Share on other sites

  • 0

I still have a question regarding this. I don't get it to work. Having experimented a bit I realized that I'm doing something wrong with the field id.

This is my code (in a HTML Block):

<script>

var FirstDate=new Date("[@calcfield:1]");
var SecondDate=new Date("[@calcfield:2]");

document.write(FirstDate);
document.getElementById("EarliestDate").value = FirstDate;

</script>
Everything works as expected until the last line: I have EarliestDate as display only, but it shows up empty when I run the code, whereas FirstDate is nonempty.

Obviously, I'm making some kind of silly mistake here, but I can't figure out what it is.

Again, I would be really grateful if anybody could help me out.

Thanks!

Bjorn

Link to comment
Share on other sites

  • 0

What is the URL of the page? Since the form element is display only there is no ID for the field to refer to. You need to change the form element to either hidden field or text field. If you would like to have it as disabled then you can use this line of the code to gray out the date text field:

document.getElementById("EditRecordEarliestDate").disabled="true";

Link to comment
Share on other sites

  • 0

Just to report back how I did. It's probably not the most beautiful code, and may contain some redundancies, but maybe it can help somebody.

After som experiementation I put this into an HTML block below the EarliestDate field. (Note that the variable names are a bit awkward. EarliestDate is both a javascript variable and a Caspio field.)

I also added a section to present the date in Swedish date format, or, more accurately, Caspio support helped me with this.

<script>

var dates = [
new Date("[@calcfield:1]"),
new Date("[@calcfield:2]"),
new Date("[@calcfield:3]"),
new Date("[@calcfield:4]"),
new Date("[@field:Date5*]")];

var EarliestDate='';

for (i=0; i<5; i++)
{
   if (isNaN(dates[i].getTime()))
   {
   dates[i]='';
   }
   else
   {    
   EarliestDate=dates[i];
   }
}

for (i=0; i<5; i++)
  {
  if (dates[i]<EarliestDate && dates[i] !='')
   {
    EarliestDate=dates[i];
    };
  }

var ar=(EarliestDate.getFullYear()).toString();
var man=(EarliestDate.getMonth()+1).toString();
var dag=(EarliestDate.getDate()).toString();

if (man<10)
  {
  man='0'+man;
  }
if (dag<10)
  {
  dag='0'+dag;
  }


if (EarliestDate == '')
{ 
document.getElementById("EditRecordEarliestDate").value = '';
}
else
{ 
document.getElementById("EditRecordEarliestDate").value = ar+"-"+man+"-" +dag;
}
</script>
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Answer this question...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
×
×
  • Create New...