如何在两个日期之间找到生日,无论年份如何 [英] How to find Birthdays between two Dates regardless of year
本文介绍了如何在两个日期之间找到生日,无论年份如何的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有记录列表,我必须在两个给定日期之间提取生日,无论一年。
也就是说,我希望生日落在日期之间,比如2015-12-01和2015-12-31
查询之间的简单检查是否出生日期是否在这两者之间。
请帮助...
谢谢
I have list of records from which I have to extract Birthdays between two given dates, regardless of the year.
That is, I want the birthdays falls between dates, say 2015-12-01 and 2015-12-31
The simple between Query checks whether the Date of Birth fields falls between these two or not.
Please help...
Thanks
推荐答案
使用 DATEPART
并提取你想要的月份:SQL Server DATEPART()函数 [ ^ ]
Use DATEPART
and extract the month you want : SQL Server DATEPART() Function[^]
您可以使用linq尝试这种方式:
You can try this way using linq :
DateTime fromDate = DateTime.Parse("2015-12-01");
DateTime toDate = DateTime.Parse("2015-12-31");
var finalValue = from a in yourlist where (a.DOB.Date >= fromDate.Date && a.DOB.Date <= toDate.Date) select a;
一个小型演示:
One small demo :
//User Class
public class User
{
public string Name { get; set; }
public DateTime DOB { get; set; }
}
//Create a list with values :
List<User> lst = new List<User>();
lst.Add(new User{ Name = "Value 0", DOB = Convert.ToDateTime("2015-11-05") });
lst.Add(new User{ Name = "Raj 0", DOB = Convert.ToDateTime("2015-12-01") });
lst.Add(new User{ Name = "Raj 1", DOB = Convert.ToDateTime("2015-12-02") });
lst.Add(new User{ Name = "Raj 2", DOB = Convert.ToDateTime("2015-12-03") });
lst.Add(new User{ Name = "Raj 3", DOB = Convert.ToDateTime("2015-12-31") });
//then write filter code :
DateTime fromDate = DateTime.Parse("2015-12-01");
DateTime toDate = DateTime.Parse("2015-12-31");
var finalValue = from a in lst where (a.DOB.Date >= fromDate.Date && a.DOB.Date <= toDate.Date) select a;
//here in finalValue you will see filtered data.
祝你好运。
Good luck.
以下解决方案将有效
Below solution will work
using System;
using System.Collections.Generic;
using System.Linq;
public class Person
{
public string PersonName { get; set; }
public DateTime DateOfBirth { get; set; }
}
public class Program
{
public static void Main()
{
//Create a list with values :
List<Person> lst = new List<Person>();
lst.Add(new Person{ PersonName = "Person 1", DateOfBirth = Convert.ToDateTime("2015-6-05") });
lst.Add(new Person{ PersonName = "Person 2", DateOfBirth = Convert.ToDateTime("2014-05-01") });
lst.Add(new Person{ PersonName = "Person 3", DateOfBirth = Convert.ToDateTime("2010-07-02") });
lst.Add(new Person{ PersonName = "Person 4", DateOfBirth = Convert.ToDateTime("2005-08-03") });
lst.Add(new Person{ PersonName = "Person 5", DateOfBirth = Convert.ToDateTime("2000-09-20") });
lst.Add(new Person{ PersonName = "Person 6", DateOfBirth = Convert.ToDateTime("2000-06-15") });
lst.Add(new Person{ PersonName = "Person 7", DateOfBirth = Convert.ToDateTime("2000-06-10") });
lst.Add(new Person{ PersonName = "Person 8", DateOfBirth = Convert.ToDateTime("2000-08-21") });
//then write filter code :
DateTime fromDate = DateTime.Parse("2055-06-15");
DateTime toDate = DateTime.Parse("1015-08-20");
var finalValue = from a in lst where (
(a.DateOfBirth.Date.Month > fromDate.Date.Month || (a.DateOfBirth.Date.Month == fromDate.Date.Month && a.DateOfBirth.Date.Day >= fromDate.Date.Day) )
&&
(a.DateOfBirth.Date.Month < toDate.Date.Month || (a.DateOfBirth.Date.Month == toDate.Date.Month && a.DateOfBirth.Date.Day <= toDate.Date.Day))
)
select a;
foreach( var p in finalValue){
Console.WriteLine(p.PersonName);
}
}
}
这篇关于如何在两个日期之间找到生日,无论年份如何的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文