Get Days date betwen StartDate and End Date in all all item

web dev asked on January 4, 2019 15:21

Hello kentico dev in my repeater i have field NewsStartDay and NewsEndDate i i create 3 newsItem to show data ==> exmple

so my question is how to make a loop to show days of newsitem (from start to end ) thanks

Correct Answer

Brenden Kehren answered on January 4, 2019 16:12

In your transformation you call the GetDates() function. Your full transformation would look something like this:

public string GetDates()
{
    System.Collections.Generic.List<string> allDates = new System.Collections.Generic.List<string>();
    DateTime startingDate = Eval<DateTime>("NewsStartDay");
    DateTime endingDate = Eval<DateTime>("NewsEndDay");

    for (DateTime date = startingDate; date <= endingDate; date = date.AddDays(1))
    {
        allDates.Add(date.ToShortDateString());
    }

    return allDates.Join(", ").ToString();
}

<div>
    <h2><%# Eval("NewsName") %></h2>
    <p><%# GetDates() %></p>
    <p><%# Eval("NewsSummary") %></p>
</div>
0 votesVote for this answer Unmark Correct answer

Recent Answers


Brenden Kehren answered on January 4, 2019 15:24

Why don't you just use the publish to and publish from dates on the page type? This is already built in and you don't need any additional coding or setup for it.

0 votesVote for this answer Mark as a Correct answer

web dev answered on January 4, 2019 15:30

thanks Brenden Kehren but my question is how to make a loop and count days for all newsitem(from start to end ) like this structure(Mon jan.2,tue jan.3 ,wed jan.4 ,.....)

0 votesVote for this answer Mark as a Correct answer

Brenden Kehren answered on January 4, 2019 15:44

Something like this should work in your transformation.

public string GetDates()
{
    System.Collections.Generic.List<string> allDates = new System.Collections.Generic.List<string>();
    DateTime startingDate = Eval<DateTime>("NewsStartDay");
    DateTime endingDate = Eval<DateTime>("NewsEndDay");

    for (DateTime date = startingDate; date <= endingDate; date = date.AddDays(1))
    {
        allDates.Add(date.ToShortDateString());
    }

    return allDates.Join(", ").ToString();
}
0 votesVote for this answer Mark as a Correct answer

web dev answered on January 4, 2019 16:00 (last edited on January 4, 2019 16:03)

Thanks Brenden Kehreni put it in repeater but i cant see the value or the result how i can display the fucntion

0 votesVote for this answer Mark as a Correct answer

   Please, sign in to be able to submit a new answer.