Jump to content

How to Search from 1 hour before Now Onwards


Recommended Posts

I have a date/time field. Caspio has the option to search from NOW onwards.

I would like to search from 1 hour before now onwards.

I could use Javascript to determine the current time and subtract an hour. Then add this to the Equal and Greater to form.

1) How can you add HTML to the filter page on datapage?

2) How can I tell Javascript to populate a criteria field of a field

This is the Javascript I have to determine the current date/time. I just need to finish it and get the two above questions and remove one hour from the time (I'm currently cheating and just showing the current hour)

Any help appreciated.

Elliot

<script type="text/javascript">
/* Declaration and initialization */
Stamp = new Date();
var v_TimeStamp;
Hours = Stamp.getHours()
Mins = Stamp.getMinutes();

/* Attach a prefix with digit '0' if the minutes is less than 10. */
if (Mins < 10) {
Mins = "0" + Mins;
}

/* Construct the value of the v_TimeStamp variable in the format m/d/yyyy hh:mm */
v_TimeStamp=('' + (Stamp.getMonth() + 1) +"/"+Stamp.getDate()+ "/"+Stamp.getFullYear() + ' ' + Hours + ":00");

/* Field name - TimeofView is referred with a prefix - EditRecord */

var v_drop = document.getElementsByName("InsertRecordburst_date")[0];
if (v_drop)
{
   var new_option = document.createElement('option');
        new_option.value = v_TimeStamp;
        new_option.text = v_TimeStamp;
        new_option.selected = true;
   try
   {
   v_drop.add(new_option,null);
   }catch(v_ex){}   
} 

</script>
Link to comment
Share on other sites

If you want to search the time after one hour before system time, select the time field from available fields at first. On the Configure Search Fields, you can select "Greater Than or Equal" from Comparison type and select "Date and Time" from Precision. Insert following code to Header :

<SCRIPT LANGUAGE="JavaScript">
function newTime()
{
/* Declaration and initialization */
Stamp = new Date();
var v_TimeStamp;
Hours = Stamp.getHours() - 1; //get one hour earlier than present time
Mins = Stamp.getMinutes();

/* Attach a prefix with digit '0' if the minutes is less than 10. */
if (Mins < 10) {
   Mins = "0" + Mins;
}

/* Construct the value of the v_TimeStamp variable in the format m/d/yyyy hh:mm */
v_TimeStamp=('' + (Stamp.getMonth() + 1) +"/"+Stamp.getDate()+ "/"+Stamp.getFullYear() + ' ' + Hours + ":" + Mins);

document.getElementById("Value1_1").value = v_TimeStamp;  //Set Time field
}

window.onload = newTime; 
</SCRIPT>
"Value1_1" in the code is the id of time field. Please replace it to a correct one.

The time field in you search page will be equal to one hour earlier than present time. :)

Link to comment
Share on other sites

Sorry, my method is not based on filtering data based on pre-defined criteria. You can have regular search, and make to submit the search automatically. Here is the code:

function newTime()

{

/* Declaration and initialization */

Stamp = new Date();

var v_TimeStamp;

Hours = Stamp.getHours() - 1; //get one hour earlier than present time

Mins = Stamp.getMinutes();

/* Attach a prefix with digit '0' if the minutes is less than 10. */

if (Mins < 10) {

Mins = "0" + Mins;

}

/* Construct the value of the v_TimeStamp variable in the format m/d/yyyy hh:mm */

v_TimeStamp=('' + (Stamp.getMonth() + 1) +"/"+Stamp.getDate()+ "/"+Stamp.getFullYear() + ' ' + Hours + ":" + Mins);

document.getElementById("Value1_1").value = v_TimeStamp; //Set Time field

}

function f_submit()

{

document.getElementById("caspioform").submit();

}

window.onload = function()

{

newTime() ; f_submit();

}

Link to comment
Share on other sites

I had an email response and here are some things cleared up by the helpful guys at Caspio:

Value1_2 = Criteria2

I used a search page (not predefined ) and then auto submitted with the following:

window.setTimeout('document.getElementById("caspioform").submit()',500);

I also had to change the code as it was giving me a 0 numbers (eg. 1 hour before 00:20 should be 23:20 but it was giving me -1:20...)

<SCRIPT LANGUAGE="JavaScript">
function newTime()
{
/* Declaration and initialization */
var olddate = new Date(); // create current date
var subbed = new Date(olddate - 2*60*60*1000); // subtract 2 hours

var v_TimeStamp;
Hours = subbed.getHours(); 
Mins = subbed.getMinutes();

/* Attach a prefix with digit '0' if the minutes is less than 10. */
if (Mins < 10) {
   Mins = "0" + Mins;
}

/* Construct the value of the v_TimeStamp variable in the format m/d/yyyy hh:mm */
v_TimeStamp=(''+subbed.getDate()+ "/" + (subbed.getMonth() + 1) +"/"+subbed.getFullYear() + ' ' + Hours + ":" + Mins);

document.getElementById("Value1_2").value = v_TimeStamp;  //Set Time field
}

window.onload = newTime; 
window.setTimeout('document.getElementById("caspioform").submit()',500);


</SCRIPT>

I also had to switch the date format around so it played well with UK date format.

Hope that helps other stoo - thanks to Caspio for the help too

Link to comment
Share on other sites

Thats a nifty variable. It works in caspio too as a parameter.

I really wanted to use it in the predefined search parameters page but couldn't work out how to apply logic to it.

IE. [@cbTimestamp] - 1hour

The way I'm auto-submitting a search page is a bit naff. I'm developing a mobile app and on loading it has to run the client-side javascript first. It would be much much better if I could use the predfined page and add some logic with the above code than o it all in Javascript - if you have any ideas I'd be dead chuffed to hear them.

Cheers

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
Reply to this topic...

×   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...
 Share

×
×
  • Create New...