不多啰嗦,直接上代码。
DateTime endTime;
TimeSpan time;
IEnumerator ITime(ushort year, byte month, byte day, byte hour, byte min, byte second)
{
endTime = new DateTime(year, month, day, hour, min, second);
while (true)
{
time = endTime - DateTime.Now;
string[] strs = time.ToString().Split('.');
string ttt = "";
if (strs.Length == 3)
{
/*时间相差一天以上,
strs[0]是相差的天数,
strs[1]是相差的时:分:秒,
strs[2]是相差的毫秒*/
string[] t = strs[1].Split(':');
ttt = string.Format("{0}:{1}:{2}", int.Parse(strs[0]) * 24 + int.Parse(t[0]), t[1], t[2]);
}
else if (strs.Length == 2)
{
/*时间相差一天以内
strs[0]是相差的时:分:秒,
strs[1]是相差的毫秒*/
ttt = strs[0];
}
timeText.text = ttt;
yield return 0;
}
}
endTime 是截至时间
TimeSpan类 是时间差,也就是需要倒计时的这段时间。