[SalesForce] Using soql query how to get all events from startdatetime to enddatetime in apex

Hi i want to get the all the events in between startdatetime to enddatetime.but i am not getting the records.

My code is:

@RemoteAction
    public static string eventdata(Datetime starts,Datetime ends){
 for(Event evnt: [select Id, Subject, isAllDayEvent, StartDateTime, EndDateTime from Event where  ownerid=:userinfo.getUserId() and startdatetime>=:starts and enddatetime<=:ends]){
}
}

in the vfpage:function getEventData()
{

        // records are retrieved from soql database
        Visualforce.remoting.Manager.invokeAction(
        '{!$RemoteAction.WisconsinCalendarController.eventdata}', 
            // controller and method names
            start.toUTCString(),
            end.toUTCString(),
            function(result, event)
            {
                console.log(result);
                if (event.status) 
                {

In the select query i am not getting values.

The values of starts is 2016-04-22 00:00:00 ends is 2016-04-30 00:00:00
I tried in the query editor if the starts and ends value format like 2016-04-22T00:00:00Z then i am getting the values but in the coding i give starts and ends values like above format i am getting Invalid date/time: 2016-04-21T20:00:00Z

Best Answer

You are trying to pass String variables for Datetime parameters. Change your method to accept String parameters instead. In order to be able to use Datetime.valueOf, you will need to use a format like 2016-04-22 00:00:00 instead. You will then need to perform the conversion to Datetime yourself:

public static void myMethod(String startTime, String endTime)
{
    Datetime startDt = Datetime.valueOfGmt(startTime);
    Datetime.endDt = Datetime.valueOfGmt(endTime);
    // other logic
}