Click here to Skip to main content
15,895,667 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
How do i split a string so its becomes [0] [1] on runtime in loop

JavaScript
var str = 'Hello, World, etc';
var myarray = str.split(',');

for(var i = 0; i < myarray.length; i++)
{
   alert(myarray[i]);
}


How do i do like this but with my code so the first time the loop runs take both values and not first value and then second time take second value , both values on same time?

EventDate and EndDate are two columns from my List there i have values in.

JavaScript
function onQuerySucceeded() {

    var startCon = listItemCustom + "T" + "08" + ":00" + "Z";
    var endCon = listItemtoDate + "T" + "17" + ":00" + "Z";
    var dtstartCon = new Date(startCon);
    var dtEndCon = new Date(endCon);
    var convertUtctoFromtime = new Date(dtstartCon.getTime() + dtstartCon.getTimezoneOffset() * 60 * 1000);
    var start = new Date(convertUtctoFromtime);
    var convertUtcToTime = new Date(dtEndCon.getTime() + dtEndCon.getTimezoneOffset() * 60 * 1000);
    var end = new Date(convertUtcToTime);

    var listItemEnumerator = collListItem.getEnumerator();
    while (listItemEnumerator.moveNext()) {
        var oListItem = listItemEnumerator.get_current();
        var listEventDate = oListItem.get_item('EventDate');
        var listEventdt = new Date(listEventDate);
        var listEndDate = oListItem.get_item('EndDate');
        var listEventEnddt = new Date(listEndDate);

        var string = listEventdt + "," + listEventEnddt;
        var res = string.split(",");


        while (end.getTime() >= start.getTime()) {

            var firstnewDate = (start.setTime(start.getTime()));
            var firstNewDt = new Date(firstnewDate);
            var newDate = start.setTime(start.getTime() + 30 * 60 * 1000);
            start = new Date(newDate);

            for (var i = 0; i < res.length; i++) {


                var date = res[i];
                var dt = new Date(date);
                var date2 = res[i];
                var dt2 = new Date(date2);
               

             if (firstNewDt.getTime() != dt.getTime() && start.getTime() != dt2.getTime()) {
                 alert(firstNewDt + " " + start);
             } else {

                 alert("Busy");
             }

        }

        }

    }
}


when running the for loop first time it will put the same value in dt.getTime() as with dt2.getTime() i want to take the value from EventDate and EndDate on same time when the loop runs, and not first take value from EvenDate and put them in both start and end time, and second time the loop runs it will put the endDate in dt.geTime() and dt2.getTime(), but i want to have it like when the loops runs take the value from the split [0] and [1] and put value from [0] in dt.getTime() and [1] in dt2.geTime() right now it takes [0] on both-
Posted
Updated 4-Dec-14 1:58am
v3
Comments
ZurdoDev 4-Dec-14 7:38am    
What?
Kurac1 4-Dec-14 7:43am    
My second code will not work the same as the first one because i have an if statement inside it, it will never display busy
ZurdoDev 4-Dec-14 8:02am    
OK. But what is the problem. You are not being clear. Remember, we have no concept of what you have been working on what you want it to do.

However, it seems like all you need to do is debug it and you'll see the problem and then just fix it.
Kurac1 4-Dec-14 8:30am    
i want to see if an user adds an item to the list see if the date and time exists if it exists in the list display busy to the user, if the item with that date and time dont exists in the list display for the users available dates and times to add to the list between the range 8:00 to 17:00 so it will check every 30 min
JR009 4-Dec-14 7:43am    
What are you trying to say?

1 solution

hi Kurac1,

try this,
No need to use For loop

function onQuerySucceeded() {

var startCon = listItemCustom + "T" + "08" + ":00" + "Z";
var endCon = listItemtoDate + "T" + "17" + ":00" + "Z";
var dtstartCon = new Date(startCon);
var dtEndCon = new Date(endCon);
var convertUtctoFromtime = new Date(dtstartCon.getTime() + dtstartCon.getTimezoneOffset() * 60 * 1000);
var start = new Date(convertUtctoFromtime);
var convertUtcToTime = new Date(dtEndCon.getTime() + dtEndCon.getTimezoneOffset() * 60 * 1000);
var end = new Date(convertUtcToTime);

var listItemEnumerator = collListItem.getEnumerator();
while (listItemEnumerator.moveNext()) {
var oListItem = listItemEnumerator.get_current();
var listEventDate = oListItem.get_item('EventDate');
var listEventdt = new Date(listEventDate);
var listEndDate = oListItem.get_item('EndDate');
var listEventEnddt = new Date(listEndDate);

var string = listEventdt + "," + listEventEnddt;
var res = string.split(",");


while (end.getTime() >= start.getTime()) {

var firstnewDate = (start.setTime(start.getTime()));
var firstNewDt = new Date(firstnewDate);
var newDate = start.setTime(start.getTime() + 30 * 60 * 1000);
start = new Date(newDate);

var date = res[0];
var dt = new Date(date);
var date2 = res[1];
var dt2 = new Date(date2);


if (firstNewDt.getTime() != dt.getTime() && start.getTime() != dt2.getTime()) {
alert(firstNewDt + " " + start);
} else {

alert("Busy");
}



}

}
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900