segi日期计算笔记

计算月末最后一天

/*
* 输入年月格式YM(如:201911),输入该月最后一天
*/
int monthEndDay(int YM)
{
    int endDay;
    int Y = YM / 100;
    int M = YM % 100;

    if (2 == M)
    {
        if (0 == Y % 400 || (0 == Y % 4 && 0 != Y % 100)) 
        {
            endDay = 29;
        }
        else
        {
            endDay = 28;
        }
    }
    else if (1 == M || 3 == M || 5 == M || 7 == M || 8 == M || 10 == M || 12 == M)
    {
        endDay = 31;
    }
    else
    {
        endDay = 30;
    }

    return endDay;
}

void test() {
    int ymd = 20190227;

    int y = ymd / 10000;
    int m = ymd / 100 % 100;
    int d = ymd % 100;

    cout << "ymd   : " << ymd << endl;    
    cout << "y     : " << y << endl;     
    cout << "m     : " << m << endl;     
    cout << "d     : " << d << endl;     

    int endDay = monthEndDay(y*100 + m);
    cout << "endDay: " << endDay << endl;
}

增减天数

/*
* 输入年月日格式YMD(如:20191114),输入增减天数nD,返回年月日格式
*/
int AddDays(int YMD, int nD)
{
    int endDay = 0;

    int Y = YMD / 10000;
    int D = YMD % 100;
    int M = YMD / 100 % 100 ;
    
    D += nD;
    endDay = monthEndDay(Y*100 + M);
    if ( nD > 0 )
    {
        while ( D > endDay )
        {    
            D -= endDay;
            M += 1;
            if ( M > 12  )
            {
                Y += M / 12;
                M = M % 12;
            }
            endDay = monthEndDay(Y*100 + M);
        }
    }
    else
    {    
        while (D <= 0)
        {
            M -= 1;
            if (0 == M)
            {
                Y -= 1;
                M = 12;
            }
            endDay = monthEndDay(Y * 100 + M);
            D += endDay;
        }
    }
    
    return (Y*100 + M) * 100 +  D;
}

void test_addDay() {
    int YMD = 20191114;
int addedYMD1 = AddDays(YMD, -5); int addedYMD2 = AddDays(YMD, -16); int addedYMD3 = AddDays(YMD, 10); int addedYMD4 = AddDays(YMD, 18);
cout
<< "addedYMD1: " << addedYMD1 << endl; // 20191109 cout << "addedYMD2: " << addedYMD2 << endl; // 20191029 cout << "addedYMD3: " << addedYMD3 << endl; // 20191124 cout << "addedYMD4: " << addedYMD4 << endl; // 20191202 }

增减月份

/*
* 输入年月格式YM(如:201911),和增加和减少月数nM, 返回年月格式
*/
int addMonths(int YM, int nM)
{
    if ( 0 == nM ) return YM;
    
    int Ms = YM / 100 * 12 + YM % 100 + nM;
    int Y = Ms / 12;
    int M = Ms % 12;
    if ( 0 == M )
    {
        M = 12;
        Y -= 1;
    }
    return Y * 100 + M;
}

void test_addMonths() {
    int YM = 201911;
    int addedYM = addMonths(YM, 2);
    cout << "addedYM: " << addedYM << endl;  // 201001
}

获取下一个月

/*
* 输入年月格式YM(如:201911), 返回年月格式,根据当前月推算下月
*/
int nextMonth(int YM)
{
    int Y = YM / 100; 
    int M = YM % 100;  

    if (12 == M)
    {
        M = 1;
        Y = Y + 1;
    }
    else
    {
        M = M + 1;
    }

    return (Y * 100 + M);
}

void test_nextMonth() {
    int YM = 201912;
    int next_month = nextMonth(YM);
    cout << "next_month: " << next_month << endl;  // 202001
}

计算weekDay

// 返回 0-6 分别代表周一到周日
int calcWeekDay(int ymd)
{
    int y = ymd / 10000;
    int m = ymd / 100 % 100;
    int d = ymd % 100;
    
    // 此处表示把1,2月计算到上一年的13,14月
    if (m == 1 || m == 2) {
        m += 12;
        y--;
    }

    // 基姆拉尔森计算公式
    int week = (d + 2 * m + 3 * (m + 1) / 5 + y + y / 4 - y / 100 + y / 400) % 7;

    return week;
}

 

 

 

 

 

 

 

posted @ 2019-11-14 15:03  evenleo  阅读(169)  评论(0编辑  收藏  举报