[英]Unity C#, How to addition X days to a date?
In this part of code sDate show current date 2016/11/16, Now what is best way to add 7 days to current date? 在这部分代码中sDate显示当前日期2016/11/16,现在最好的方法是将7天添加到当前日期? for example if current date is 2016/11/29 + 7 change to 2016/12/06.
例如,如果当前日期是2016/11/29 + 7更改为2016/12/06。 I'm looking for a way to addition an int value to a date.
我正在寻找一种将int值添加到日期的方法。
string Year;
string Month;
string Day;
float time;
string sDate;
void Start ()
{
Year = System.DateTime.Now.Year.ToString();
Month = System.DateTime.Now.Month.ToString();
Day = System.DateTime.Now.Day.ToString();
int Y = int.Parse (Year);
int M = int.Parse (Month);
int D = int.Parse (Day);
if (Y >= 2016 & M >= 11 & D >= 21)
{
sDate = Year + "/" + Month + "/" + Day + " | Expired";
Debug.Log (sDate);
Application.Quit ();
}
else
{
sDate = Year + "/" + Month + "/" + Day + " | Working";
Debug.Log ("System date: " + sDate);
}
}
All you need to do is use the standard DateTime function AddDays
: 您需要做的就是使用标准的DateTime函数
AddDays
:
DateTime result = original.AddDays(n);
where original
is the original date and n
is the number of days you want to add. 其中
original
是原始日期, n
是要添加的天数。
I'd also check the rest of the documentation on the DateTime
structure as there are a lot simpler ways of doing what you are trying here. 我还将检查有关
DateTime
结构的其余文档,因为这里有很多简单的方法可以完成您尝试的操作。 As mentioned in the comments you can construct a DateTime object from it's components: 如注释中所述,您可以从其组件构造DateTime对象:
DateTime referenceDate = new DateTime(2016, 11, 15);
and then do comparisons on that: 然后对此进行比较:
if (testDate >= referenceDate)
{
// Do something
}
etc. 等等
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.