Reputation: 559
I am working on Facebook Birthdays List using Graph Api
By this Url Im Getting All My Friends Birthdays List
"https://graph.facebook.com/{0}?fields=friends.fields(birthday,id,name)&access_token={1}", fbid, acctocken"
After Getting Responce My Code is Like this
var response = e.Result;
var jsonData = JsonConvert.DeserializeObject<RootObject>(response);
jsonData.friends.data = jsonData.friends.data.OrderBy(BdayItems => BdayItems.birthday).ToList();
By Using the Above Linq Query I am sorting My Friends List By the Birthdays and I am display All My Friends Birthdays in my ListBox.
But I need More Like
I want to Display Only Next 30 days Birthdays from today
How can Make this in Easy way By using Linq Query or Any Other
Upvotes: 0
Views: 866
Reputation: 59
Cover all cases to filter out birthdays including December case for January
static void Main(string[] args)
{
List<DateTime> birthdays = new List<DateTime>() {
new DateTime(1977,1,29),
new DateTime(1977,1,30),
new DateTime(1977,1,31)
};
var daysFrom = 30;
var start = new DateTime(DateTime.Now.Year,DateTime.Now.Month,DateTime.Now.Day);
start = new DateTime(2020, 12, 31); // border test case
var last = start.AddDays(daysFrom);
var yearSwitch = last.Year - start.Year;
var res = birthdays.Where(bday =>
{
var bn = new DateTime(start.Year, bday.Month, bday.Day);
if (bday.DayOfYear < daysFrom)
{
bn = bn.AddYears(yearSwitch);
}
return bn >= start && bn <= last;
}
).ToList();
Console.WriteLine("List:{0}", string.Join(",", res));
}
Upvotes: 0
Reputation: 439
Assuming BdayItems.birthday
is of type string
you could do something like:
jsonData.friends.data = jsonData.friends.data.Where(BdayItems => BdayItems.birthday != null
&& DateTime.Parse(BdayItems.birthday) >= DateTime.Today
&& DateTime.Parse(BdayItems.birthday) <= DateTime.Today.AddDays(30))
.OrderBy(BdayItems => DateTime.Parse(BdayItems.birthday))
.ToList();
EDIT: I was dumb and didn't account for before today. Also switched to assume BdayItems.birthday
is of type string
instead of DateTime
. However, you still will have to catch errors in case DateTime.Parse()
fails.
Upvotes: 1