I have a DateTime object with a person's birthday. I created this object using the person's year, month and day of birth, in the following way:
我有一个人的生日的DateTime对象。我使用人的年,月和出生日创建此对象,方法如下:
DateTime date = new DateTime(year, month, day);
I would like to know how many days are remaining before this person's next birthday. What is the best way to do so in C# (I'm new to the language)?
我想知道在这个人下一个生日之前剩下多少天。在C#中这样做的最佳方法是什么(我是语言新手)?
6 个解决方案
#1
// birthday is a DateTime containing the birthday
DateTime today = DateTime.Today;
DateTime next = new DateTime(today.Year,birthday.Month,birthday.Day);
if (next < today)
next = next.AddYears(1);
int numDays = (next - today).Days;
This trivial algorithm fails if the birthday is Feb 29th. This is the alternative (which is essentially the same as the answer by Seb Nilsson:
如果生日是2月29日,这个简单的算法就会失败。这是另一种选择(与Seb Nilsson的答案基本相同:
DateTime today = DateTime.Today;
DateTime next = birthday.AddYears(today.Year - birthday.Year);
if (next < today)
next = next.AddYears(1);
int numDays = (next - today).Days;
#2
Using today's year and the birthday's month and day will not work with leap-years.
使用今天的年份和生日的月份和日期不适用于闰年。
After a little bit of testing, this is what I get to work:
经过一些测试,这就是我的工作:
private static int GetDaysUntilBirthday(DateTime birthday) {
var nextBirthday = birthday.AddYears(DateTime.Today.Year - birthday.Year);
if(nextBirthday < DateTime.Today) {
nextBirthday = nextBirthday.AddYears(1);
}
return (nextBirthday - DateTime.Today).Days;
}
Tested with 29th February on a leap-year and also when the birthday is the same day.
2月29日在闰年和生日当天进行测试。
#3
This is based off of Philippe Leybaert's answer above, but handles one additional edge case that I don't see accounted for in any of the previous answers.
这是基于Philippe Leybaert在上面的回答,但处理了一个额外的边缘情况,我在以前的任何答案中都没有看到这个问题。
The edge case I'm addressing is when the birthday is on a leap day, the birthday is in the past for the current year, and the current year isn't a leap year but the next year is.
我正在解决的边缘情况是生日是闰日,生日是过去一年,当年不是闰年,而是明年。
The current answer provided will result in one fewer day as it sets "next" to Feb 28 of the current year and then adds a year making the date Feb 28 of a leap year (which isn't correct). Changing one line handles this edge case.
提供的当前答案将导致少一天,因为它设置为当前年度的2月28日“下一个”,然后增加一年,使得闰年2月28日(这是不正确的)。更改一行可处理此边缘情况。
DateTime today = DateTime.Today;
DateTime next = birthday.AddYears(today.Year - birthday.Year);
if (next < today)
{
if (!DateTime.IsLeapYear(next.Year + 1))
next = next.AddYears(1);
else
next = new DateTime(next.Year + 1, birthday.Month, birthday.Day);
}
int numDays = (next - today).Days;
Update: Edited per Philippe's pointing out that my code had a rather sizable flaw.
更新:根据Philippe的编辑指出我的代码有一个相当大的缺陷。
#4
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
DateTime dt1 = DateTime.Parse("09/08/2012");
DateTime dt2 = DateTime.Parse(DateTime.Now.ToString());
int days = (dt2 - dt1).Days;
Console.WriteLine(days);
double month = (dt2 - dt1).Days / 30;
Console.WriteLine(month);
double year = (dt2 - dt1).Days / 365;
Console.WriteLine(year);
Console.Read();
}
}
}
#5
Try this method
试试这个方法
private int GetDaysBeforeBirthday(DateTime birthdate)
{
DateTime nextBday = new DateTime(DateTime.Now.Year, birthdate.Month, birthdate.Day);
if (DateTime.Today > nextBday)
nextBday = nextBday.AddYears(1);
return (nextBday - DateTime.Today).Days;
}
just pass your birthdate and it will return the remaining days before your next birthday
只要通过您的生日,它将返回您下一个生日前的剩余天数
#6
DateTime Variable = DateTime.Now;
int NumOfDaysTillNextMonth = 0;
while (Variable < Comparer) //Comparer is just a target datetime
{
Variable = Variable.AddDays(1);
NumOfDaysTillNextMonth++;
}
Had to do this just now for a program. It's simple enough as opposed to the other methods if all you need is an integer of days left.
不得不为一个程序做这个。如果您需要的是剩余的整数天,那么它与其他方法相比就足够简单了。
#1
// birthday is a DateTime containing the birthday
DateTime today = DateTime.Today;
DateTime next = new DateTime(today.Year,birthday.Month,birthday.Day);
if (next < today)
next = next.AddYears(1);
int numDays = (next - today).Days;
This trivial algorithm fails if the birthday is Feb 29th. This is the alternative (which is essentially the same as the answer by Seb Nilsson:
如果生日是2月29日,这个简单的算法就会失败。这是另一种选择(与Seb Nilsson的答案基本相同:
DateTime today = DateTime.Today;
DateTime next = birthday.AddYears(today.Year - birthday.Year);
if (next < today)
next = next.AddYears(1);
int numDays = (next - today).Days;
#2
Using today's year and the birthday's month and day will not work with leap-years.
使用今天的年份和生日的月份和日期不适用于闰年。
After a little bit of testing, this is what I get to work:
经过一些测试,这就是我的工作:
private static int GetDaysUntilBirthday(DateTime birthday) {
var nextBirthday = birthday.AddYears(DateTime.Today.Year - birthday.Year);
if(nextBirthday < DateTime.Today) {
nextBirthday = nextBirthday.AddYears(1);
}
return (nextBirthday - DateTime.Today).Days;
}
Tested with 29th February on a leap-year and also when the birthday is the same day.
2月29日在闰年和生日当天进行测试。
#3
This is based off of Philippe Leybaert's answer above, but handles one additional edge case that I don't see accounted for in any of the previous answers.
这是基于Philippe Leybaert在上面的回答,但处理了一个额外的边缘情况,我在以前的任何答案中都没有看到这个问题。
The edge case I'm addressing is when the birthday is on a leap day, the birthday is in the past for the current year, and the current year isn't a leap year but the next year is.
我正在解决的边缘情况是生日是闰日,生日是过去一年,当年不是闰年,而是明年。
The current answer provided will result in one fewer day as it sets "next" to Feb 28 of the current year and then adds a year making the date Feb 28 of a leap year (which isn't correct). Changing one line handles this edge case.
提供的当前答案将导致少一天,因为它设置为当前年度的2月28日“下一个”,然后增加一年,使得闰年2月28日(这是不正确的)。更改一行可处理此边缘情况。
DateTime today = DateTime.Today;
DateTime next = birthday.AddYears(today.Year - birthday.Year);
if (next < today)
{
if (!DateTime.IsLeapYear(next.Year + 1))
next = next.AddYears(1);
else
next = new DateTime(next.Year + 1, birthday.Month, birthday.Day);
}
int numDays = (next - today).Days;
Update: Edited per Philippe's pointing out that my code had a rather sizable flaw.
更新:根据Philippe的编辑指出我的代码有一个相当大的缺陷。
#4
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
DateTime dt1 = DateTime.Parse("09/08/2012");
DateTime dt2 = DateTime.Parse(DateTime.Now.ToString());
int days = (dt2 - dt1).Days;
Console.WriteLine(days);
double month = (dt2 - dt1).Days / 30;
Console.WriteLine(month);
double year = (dt2 - dt1).Days / 365;
Console.WriteLine(year);
Console.Read();
}
}
}
#5
Try this method
试试这个方法
private int GetDaysBeforeBirthday(DateTime birthdate)
{
DateTime nextBday = new DateTime(DateTime.Now.Year, birthdate.Month, birthdate.Day);
if (DateTime.Today > nextBday)
nextBday = nextBday.AddYears(1);
return (nextBday - DateTime.Today).Days;
}
just pass your birthdate and it will return the remaining days before your next birthday
只要通过您的生日,它将返回您下一个生日前的剩余天数
#6
DateTime Variable = DateTime.Now;
int NumOfDaysTillNextMonth = 0;
while (Variable < Comparer) //Comparer is just a target datetime
{
Variable = Variable.AddDays(1);
NumOfDaysTillNextMonth++;
}
Had to do this just now for a program. It's simple enough as opposed to the other methods if all you need is an integer of days left.
不得不为一个程序做这个。如果您需要的是剩余的整数天,那么它与其他方法相比就足够简单了。