uestcjerry

积累 思考

计算年月日函数

一:判断是否为闰年

bool isUnNormalYear(const long long year)
{
    if ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0))
        return true;
    else
        return false;
}

二:判断某年某月某日为星期几

int CaculateWeekDay(long long y, long long m, long long d)
{
    if (m == 1 || m == 2) {
        m += 12;
        y--;
    }
    int iWeek = static_cast<int>((d + 2 * m + 3 * (m + 1) / 5 + y + y / 4 - y / 100 + y / 400) % 7);

    switch (iWeek)
    {
        case 0: printf("星期一\n"); break;
        case 1: printf("星期二\n"); break;
        case 2: printf("星期三\n"); break;
        case 3: printf("星期四\n"); break;
        case 4: printf("星期五\n"); break;
        case 5: printf("星期六\n"); break;
        case 6: printf("星期日\n"); break;
    }
    return iWeek + 1;
}

三:计算A(年月日)到B(年月日)之间相差天数

long long day_distance(long long year1, long long month1, long long day1,
                         long long year2, long long month2, long long day2)
{
    long long nd, nm, ny;       //new_day, new_month, new_year  
    long long od, om, oy;       //old_day, oldmonth, old_year  

    nm = (month2 + 9) % 12;
    ny = year2 - nm / 10;
    nd = 365 * ny + ny / 4 - ny / 100 + ny / 400 + (nm * 306 + 5) / 10 + (day2 - 1);

    om = (month1 + 9) % 12;
    oy = year1 - om / 10;
    od = 365 * oy + oy / 4 - oy / 100 + oy / 400 + (om * 306 + 5) / 10 + (day1 - 1);

    return od - nd;
}
阅读更多
版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/u014787464/article/details/51064452
文章标签: 年月日计算
个人分类: 算法
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

加入CSDN,享受更精准的内容推荐,与500万程序员共同成长!
关闭
关闭