C# 计算距生日的剩余天数?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1170257/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Calculate days remaining to a birthday?
提问by Roee Adler
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# 中这样做的最佳方法是什么(我是该语言的新手)?
采纳答案by Philippe Leybaert
// 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;
回答by Leon Tayson
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
只需传递您的生日,它就会返回您下一个生日前的剩余天数
回答by Seb Nilsson
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 日以及生日同一天进行测试。
回答by Adrian
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.
刚才为了一个程序不得不这样做。如果您只需要剩余的整数天,那么与其他方法相比,它足够简单。
回答by J Stuart
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 指出我的代码有一个相当大的缺陷进行编辑。
回答by Mayur Narula
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();
}
}
}