c# - Finding next 30 days from today -


for following json:

{   "id": "100001","emp": {"data": [      {         "birthday": "08/14/1987",          "id": "52442"       },        {         "id": "52554"       },        {         "id": "54281"       },        {         "birthday": "04/08/1986",          "id": "54353"       },        {         "id": "58667"       },        {         "birthday": "12/20",          "id": "61435"       },        {         "id": "62794"       },        {         "id": "64577"       },        {         "birthday": "08/12",          "id": "65151"       },        {         "birthday": "11/15/1988",          "id": "66075"       },       {         "birthday": "08/02",          "id": "68282"       },        {         "birthday": "05/01",          "id": "70120"       },        {         "birthday": "12/24/1989",          "id": "74611"       },        {         "birthday": "06/18",          "id": "10293"       }   ],   } } 

some emp objects don't have birthday property , birthdays doesn't have year defined.

now task next 30 emp objects birthdays starting today

my code this

var response = e.result; var jsondata = jsonconvert.deserializeobject<rootobject>(response); jsondata.emp.data = jsondata.friends.data.where(bdayitems => (bdayitems.birthday != null && datetime.parse(bdayitems.birthday)<= datetime.today.adddays(30))).orderby(bdayitems => bdayitems.birthday).tolist(); 

i tried remove year using .addyear(-1) in linq query.

but i'm not getting next 30 day, getting emp birthday list, need need next 30 days today

i think you'll need convert birthdays current year first. adding difference between birthdate year , current year. or if care specific way of handling leap years, use datetime constructor , own leap year handling algorithm.

also, answer addressed, should ensure birthday (in current year) @ or after current date.

as requested, here's example of 1 way this. uses leap year handling of addyears.

jsondata.friends.data.where(bdayitems => {     if (bdayitems.birthday != null)     {         var originalbirthday = datetime.parse(bdayitems.birthday);         var today = datetime.today; //storing prevents race condition.         var birthdaythisyear = originalbirthday.addyears(today.years - originalbirthday.years);         var thirtydaysfromnow = today.adddays(30);          return birthdaythisyear >= today && birthdaythisyear <= thirtydaysfromnow;     }      return false; }); 

i'm using phone, haven't tested it. make sure logic correct requirements before using in real application. reccomend use unit tests ensure functions correctly.


Comments