developer tip

Excel 일련 날짜 번호를 .NET DateTime으로 변환하려면 어떻게합니까?

optionbox 2020. 11. 6. 08:05
반응형

Excel 일련 날짜 번호를 .NET DateTime으로 변환하려면 어떻게합니까?


Excel 일련 날짜에서 .NET 날짜 시간으로 어떻게 변환합니까?

예를 들어 39938은 2009 년 5 월 5 일입니다.


39938은 1900 년 1 월 1 일 이후의 날짜 수는 어디입니까?

이 경우 프레임 워크 라이브러리 함수를 사용하십시오 DateTime.FromOADate().
이 함수 모든 세부 사항을 캡슐화 하고 경계 검사를 수행합니다.

역사적 가치에 대해 가능한 구현은 다음과 같습니다.

(씨#)

public static DateTime FromExcelSerialDate(int SerialDate)
{
    if (SerialDate > 59) SerialDate -= 1; //Excel/Lotus 2/29/1900 bug   
    return new DateTime(1899, 12, 31).AddDays(SerialDate);
}

VB

Public Shared Function FromExcelSerialDate(ByVal SerialDate As Integer) As DateTime
    If SerialDate > 59 Then SerialDate -= 1 ''// Excel/Lotus 2/29/1900 bug
    Return New DateTime(1899, 12, 31).AddDays(SerialDate)
End Function

[업데이트] :
흠 ... 그것의 빠른 테스트는 실제로 이틀 쉬는 것을 보여줍니다. 차이점이 어디인지 확실하지 않습니다.

좋습니다. 이제 문제가 해결되었습니다. 자세한 내용은 주석을 참조하십시오.


예를 들어 FromOADate 메서드를 사용하면 더 간단합니다 .

DateTime dt = DateTime.FromOADate(39938);

이 코드를 사용하면 dt 는 "05/05/2009"입니다.


void ExcelSerialDateToDMY(int nSerialDate, int &nDay, 
                          int &nMonth, int &nYear)
{
    // Excel/Lotus 123 have a bug with 29-02-1900. 1900 is not a
    // leap year, but Excel/Lotus 123 think it is...
    if (nSerialDate == 60)
    {
        nDay    = 29;
        nMonth    = 2;
        nYear    = 1900;

        return;
    }
    else if (nSerialDate < 60)
    {
        // Because of the 29-02-1900 bug, any serial date 
        // under 60 is one off... Compensate.
        nSerialDate++;
    }

    // Modified Julian to DMY calculation with an addition of 2415019
    int l = nSerialDate + 68569 + 2415019;
    int n = int(( 4 * l ) / 146097);
            l = l - int(( 146097 * n + 3 ) / 4);
    int i = int(( 4000 * ( l + 1 ) ) / 1461001);
        l = l - int(( 1461 * i ) / 4) + 31;
    int j = int(( 80 * l ) / 2447);
     nDay = l - int(( 2447 * j ) / 80);
        l = int(j / 11);
        nMonth = j + 2 - ( 12 * l );
    nYear = 100 * ( n - 49 ) + i + l;
}

다른 사람의 재능을 잘라내어 붙여 넣기 ...

이안 브라운


For 39938 do this: 39938 * 864000000000 + 599264352000000000

864000000000 represents number of ticks in a day 599264352000000000 represents number of ticks from the year 0001 to the year 1900

참고URL : https://stackoverflow.com/questions/727466/how-do-i-convert-an-excel-serial-date-number-to-a-net-datetime

반응형