programing

날짜 대 날짜 시간

lastcode 2023. 5. 27. 11:34
반응형

날짜 대 날짜 시간

저는 행사 날짜가 있어야 반품이 가능한 프로그램을 진행하고 있습니다.

나는 찾고 있습니다.Date조금도 아닌DateTime.

날짜만 반환하는 데이터 유형이 있습니까?

아니요, 없어요. DateTime날짜와 시간으로 구성된 특정 시점을 나타냅니다.그러나 속성(다른 속성)을 통해 날짜 부분을 검색할 수 있습니다.DateTime시간을 에 두고00:00:00).

및 를 통해 개별 날짜 속성을 검색할 수 있습니다.

업데이트: .NET 6의 유형DateOnly그리고.TimeOnly 날짜 또는 시간을 나타내는 소개가 있습니다.

시간 부분, 시간대, 로컬 대 utc 등에 대해 걱정하지 않고 간단한 날짜가 필요한 경우를 위해 간단한 날짜 구조를 만들었습니다.

Date today = Date.Today;
Date yesterday = Date.Today.AddDays(-1);
Date independenceDay = Date.Parse("2013-07-04");

independenceDay.ToLongString();    // "Thursday, July 4, 2013"
independenceDay.ToShortString();   // "7/4/2013"
independenceDay.ToString();        // "7/4/2013"
independenceDay.ToString("s");     // "2013-07-04"
int july = independenceDay.Month;  // 7

https://github.com/claycephus/csharp-date

유감스럽게도, 에 없습니다.Net BCL.날짜는 일반적으로 시간이 자정으로 설정된 DateTime 개체로 표시됩니다.

이는 날짜 개체의 경우 시간대 처리가 전혀 필요하지 않더라도 모든 수행 시간대 문제가 있음을 의미합니다.

래퍼 클래스를 만듭니다.이와 같은 것:

public class Date:IEquatable<Date>,IEquatable<DateTime>
    {
        public Date(DateTime date)
        {
            value = date.Date;
        }

        public bool Equals(Date other)
        {
            return other != null && value.Equals(other.value);
        }

        public bool Equals(DateTime other)
        {
            return value.Equals(other);
        }

        public override string ToString()
        {
            return value.ToString();
        }
        public static implicit operator DateTime(Date date)
        {
            return date.value;
        }
        public static explicit operator Date(DateTime dateTime)
        {
            return new Date(dateTime);
        }

        private DateTime value;
    }

그리고 어떤 것이든 폭로합니다.value너는 원한다.

Date 유형은 VB.NET에서 사용하는 DateTime 유형의 별칭입니다(int가 Integer가 되는 경우).두 유형 모두 시간 부분이 00:00:00으로 설정된 개체를 반환하는 Date 속성을 가지고 있습니다.

DateTime에는 날짜 부분을 분리하는 데 사용할 수 있는 Date 속성이 있습니다.ToString 메서드는 시간 부분이 비어 있을 때 Date 부분만 표시하는 작업도 잘 수행합니다.

DateTime 개체에는 값의 날짜 부분만 반환하는 속성이 있습니다.

    public static void Main()
{
    System.DateTime _Now = DateAndTime.Now;
    Console.WriteLine("The Date and Time is " + _Now);
    //will return the date and time
    Console.WriteLine("The Date Only is " + _Now.Date);
    //will return only the date
    Console.Write("Press any key to continue . . . ");
    Console.ReadKey(true);
}

거기에는 없다Date데이터 유형.

사용할 수 있는 방법DateTime.Date날짜를 확인할 수 있습니다.

예.

DateTime date = DateTime.Now.Date;

.NET 6에서 드디어 날짜 전용 유형을 도입하는 것 같습니다.라고 할 것입니다.DateOnly그리고 또한 있을 것입니다.TimeOnly의 BCL에 추가된 유형System네임스페이스입니다.

미리 보기 4에서 이미 사용할 수 있습니다.자세한 내용은 이 블로그 기사를 참조하십시오.

DateTime(시간 부분이 00:00:00)을 반환하고 무시할 수 있습니다.날짜는 타임스탬프 정수로 처리되므로 날짜와 정수에 있는 시간을 결합하는 것이 좋습니다.

이 경우 날짜를 사용해야 하지만 시간 값은 무시해야 합니다.

일반적으로 날짜는 시간이 다음과 같은 날짜 시간입니다.00:00:00

DateTime에 type이 ..Date을 하는 재산DateTime시간 값을 위와 같이 설정합니다.

다음 중 하나를 시도할 수 있습니다.

DateTime.Now.ToLongDateString();
DateTime.Now.ToShortDateString();

그러나 BCL에는 "날짜" 유형이 없습니다.

public class AsOfdates
{
    public string DisplayDate { get; set; }
    private DateTime TheDate;
    public DateTime DateValue 
    {
        get 
        { 
            return TheDate.Date; 
        } 

        set 
        { 
            TheDate = value; 
        } 
    }    
}

데얀과 조나스 롬홀트가 지적했듯이,

.Net 6을 가지고 있습니다.DateOnlytype은 년, 월 및 일과 같은 날짜만 나타내는 구조입니다.

DateOnly d1 = new DateOnly(2022, 5, 16);
Console.WriteLine(d1);            // 5/16/2022
Console.WriteLine(d1.Year);      // 2021
Console.WriteLine(d1.Month);     // 5
Console.WriteLine(d1.Day);       // 16
Console.WriteLine(d1.DayOfWeek); // Monday



// Manipulation
DateOnly d2 = d1.AddMonths(3);  // You can add days, months, or years. Use negative values to subtract. We are adding 3 months
Console.WriteLine(d2);     // "8/16/2022"  notice there is NO time

// You can use the DayNumber property to find out how many days are between two dates
int days = d2.DayNumber - d1.DayNumber;
Console.WriteLine($"There are {days} days between {d1} and {d2}");  //There are 92 days between 5/16/2022 and 8/16/2022

이 기사에 대한 전적인 공로는 Matt Johnson-Pint에게 있습니다.

https://devblogs.microsoft.com/dotnet/date-time-and-time-zone-enhancements-in-net-6/

언급URL : https://stackoverflow.com/questions/798121/date-vs-datetime

반응형