이 질문은 Java , JavaScript 및 PHP 에 대해서는 답변 되었지만 C #에는 해당되지 않습니다. 그렇다면 C #에서 두 날짜 사이의 일 수를 어떻게 계산할 수 있습니까?
DateTime및 TimeSpan유형에 대해 오버로드됩니다 . 모두 매우 간단합니다. -어떤 정확한 문제가 발생 했습니까?
이 질문은 Java , JavaScript 및 PHP 에 대해서는 답변 되었지만 C #에는 해당되지 않습니다. 그렇다면 C #에서 두 날짜 사이의 일 수를 어떻게 계산할 수 있습니까?
DateTime및 TimeSpan유형에 대해 오버로드됩니다 . 모두 매우 간단합니다. -어떤 정확한 문제가 발생 했습니까?
답변:
가정 StartDate하고 EndDate유형 DateTime:
(EndDate - StartDate).TotalDays
(a - b).Days총 일수에 관심이있는 경우 에도 사용할 수 있습니다 . intdouble
가장 좋은 대답은 맞지만 전체 날짜를 int로만 사용하고 날짜의 시간 구성 요소를 잊어 버린 경우 다음을 고려하십시오.
(EndDate.Date - StartDate.Date).Days
다시 가정 StartDate하고 EndDate유형 DateTime입니다.
Days365에서 멈추지 않는다 (같은 다른 속성으로 Hours, Minutes, Second다음으로 높은 특성이 시작되는 곳이다 NaX 형있는 값). 그것과 동일 TotalDays하지만 하루의 분수가없고 int대신에 돌아 옵니다 double.
.Date부분은 정말 유용합니다. 내가 분명해
나는 이것이 당신이 원하는 것을 할 것이라고 생각합니다 :
DateTime d1 = DateTime.Now;
DateTime d2 = DateTime.Now.AddDays(-1);
TimeSpan t = d1 - d2;
double NrOfDays = t.TotalDays;
DateTime xmas = new DateTime(2009, 12, 25);
double daysUntilChristmas = xmas.Subtract(DateTime.Today).TotalDays;
// Difference in days, hours, and minutes.
TimeSpan ts = EndDate - StartDate;
// Difference in days.
int differenceInDays = ts.Days; // This is in int
double differenceInDays= ts.TotalDays; // This is in double
// Difference in Hours.
int differenceInHours = ts.Hours; // This is in int
double differenceInHours= ts.TotalHours; // This is in double
// Difference in Minutes.
int differenceInMinutes = ts.Minutes; // This is in int
double differenceInMinutes= ts.TotalMinutes; // This is in double
초, 밀리 초 및 틱 단위로 차이를 얻을 수도 있습니다.
당신은 이것을 시도 할 수 있습니다
EndDate.Date.Subtract(DateTime.Now.Date).Days
저와 같은 초보자에게는이 작은 문제에 대해 간단한 줄로 샘플을 int로 변환하는 것입니다 .
int totalDays = Convert.ToInt32((DateTime.UtcNow.Date - myDateTime.Date).TotalDays);
오늘 (DateTime.UtcNow.Date)에서 원하는 날짜 (myDateTime.Date)까지의 총 일수를 계산합니다.
myDateTime이 어제이거나 오늘보다 오래된 날짜이면 양수 (+) 정수 결과가 표시됩니다.
반면에 myDateTime이 내일 또는 미래 날짜 인 경우 추가 규칙으로 인해 음의 (-) 정수 결과가 나타납니다.
행복한 코딩! ^ _ ^
시간 범위를 사용하면 많은 속성이 있으므로 문제를 해결할 수 있습니다.
DateTime strt_date = DateTime.Now;
DateTime end_date = Convert.ToDateTime("10/1/2017 23:59:59");
//DateTime add_days = end_date.AddDays(1);
TimeSpan nod = (end_date - strt_date);
Console.WriteLine(strt_date + "" + end_date + "" + "" + nod.TotalHours + "");
Console.ReadKey();
들어 a와 b이 개 같은 DateTime유형 :
DateTime d = DateTime.Now;
DateTime c = DateTime.Now;
c = d.AddDays(145);
string cc;
Console.WriteLine(d);
Console.WriteLine(c);
var t = (c - d).Days;
Console.WriteLine(t);
cc = Console.ReadLine();
먼저 나중에 리턴 할 클래스를 선언하십시오.
public void date()
{
Datetime startdate;
Datetime enddate;
Timespan remaindate;
startdate = DateTime.Parse(txtstartdate.Text).Date;
enddate = DateTime.Parse(txtenddate.Text).Date;
remaindate = enddate - startdate;
if (remaindate != null)
{
lblmsg.Text = "you have left with " + remaindate.TotalDays + "days.";
}
else
{
lblmsg.Text = "correct your code again.";
}
}
protected void btncal_Click(object sender, EventArgs e)
{
date();
}
버튼 컨트롤을 사용하여 위 클래스를 호출하십시오. 예를 들면 다음과 같습니다.
두 날짜의 차이를 얻은 다음 다음 날짜를 가져옵니다.
int total_days = (EndDate - StartDate).TotalDays
protected void Calendar1_SelectionChanged(object sender, EventArgs e)
{
DateTime d = Calendar1.SelectedDate;
// int a;
TextBox2.Text = d.ToShortDateString();
string s = Convert.ToDateTime(TextBox2.Text).ToShortDateString();
string s1 = Convert.ToDateTime(Label7.Text).ToShortDateString();
DateTime dt = Convert.ToDateTime(s).Date;
DateTime dt1 = Convert.ToDateTime(s1).Date;
if (dt <= dt1)
{
Response.Write("<script>alert(' Not a valid Date to extend warranty')</script>");
}
else
{
string diff = dt.Subtract(dt1).ToString();
Response.Write(diff);
Label18.Text = diff;
Session["diff"] = Label18.Text;
}
}