오늘 DateTime이 발생하는지 확인하는 방법은 무엇입니까?
DateTime이 '오늘'발생했는지 확인하는 더 나은 .net 방법이 있습니까?
if ( newsStory.WhenAdded.Day == DateTime.Now.Day &&
newsStory.WhenAdded.Month == DateTime.Now.Month &&
newsStory.WhenAdded.Year == DateTime.Now.Year )
{
// Story happened today
}
else
{
// Story didn't happen today
}
if (newsStory.WhenAdded.Date == DateTime.Today)
{
}
else
{
}
트릭을해야합니다.
if( newsStory.Date == DateTime.Today )
{
// happened today
}
시험
if (newsStory.Date == DateTime.Now.Date)
{ /* Story happened today */ }
else
{ /* Story didn't happen today */ }
내 솔루션 :
private bool IsTheSameDay(DateTime date1, DateTime date2)
{
return (date1.Year == date2.Year && date1.DayOfYear == date2.DayOfYear);
}
NewsStory가 DateTime도 사용하고 있었다면 Date 속성을 비교하기 만하면됩니다.
그러나 이것은 "오늘"이 실제로 의미하는 바에 따라 다릅니다. 자정 직전에 게시 된 내용은 잠시 후 "오래된"상태가됩니다. 따라서 정확한 스토리 날짜 (시간 포함, 가급적 UTC 포함)를 유지하고 24 시간 미만 (또는 기타)이 지 났는지 확인하는 것이 가장 좋습니다. 이는 간단합니다 (날짜를 뺄 수 있으므로 TotalHours가 포함 된 TimeSpan을 얻을 수 있음). 또는 TotalDays 속성).
DateTime 확장 메서드를 구현할 수 있습니다.
확장 메서드에 대한 새 클래스를 만듭니다.
namespace ExtensionMethods
{
public static class ExtensionMethods
{
public static bool IsSameDay( this DateTime datetime1, DateTime datetime2 )
{
return datetime1.Year == datetime2.Year
&& datetime1.Month == datetime2.Month
&& datetime1.Day == datetime2.Day;
}
}
}
And now, everywhere on your code, where do you want to perform this test, you should include the using:
using ExtensionMethods;
And then, use the extension method:
newsStory.WhenAdded.IsSameDay(DateTime.Now);
FYI,
newsStory.Date == DateTime.Today
will return the same compare result as coding
newsStory == DateTime.Today
where newsStory
is a DateTime
object
.NET is smart enough to determine you want to compare based on Date only and uses that for the internal Compare. Not sure why, and actually having trouble finding documentation for this behaviour.
As Guillame suggested in a comment, compare values of Date
properties:
newStory.Date == DateTime.Now.Date
Try this:
newsStory.Date == DateTime.Today
well, DateTime has a "Date" property and you could just compare based on that. But looking at the docs it seems that getting that property actually instantiates a new datetime with the time component set to midnight, so it may very well be slower than accessing each individual component, although much cleaner and more readable.
if (newsStory.ToShortDateString() == DateTime.Today.ToShortDateString()) return "Todtay";
How about
if (newsStory.DayOfYear == DateTime.Now.DayOfYear)
{ // Story happened today
}
But this will also return true for 1st January 2008 and 1st January 2009, which may or may not be what you want.
you could use DateTime.Now.DayOfYear
if (newsStory.DayOfYear == DateTime.Now.DayOfYear)
{ // story happened today
}
else
{ // story didn't happen today
}
참고URL : https://stackoverflow.com/questions/1601609/how-to-check-if-a-datetime-occurs-today
'Programming' 카테고리의 다른 글
Bash의 문자열에서 줄 바꿈을 제거하는 방법 (0) | 2020.08.22 |
---|---|
jquery를 사용하여 div 내의 텍스트 만 교체 (0) | 2020.08.22 |
Angular에서 앱 버전을 표시하는 방법은 무엇입니까? (0) | 2020.08.22 |
Git 푸시 실패,“비 빨리 전달 업데이트가 거부되었습니다.” (0) | 2020.08.22 |
다른 상태의 두 이미지를 사용하는 전환 버튼 (0) | 2020.08.22 |