如何将带日期的char转换为double

如何将带日期的char转换为double

本文介绍了如何将带日期的char转换为double?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个日期字符串:

I have a string with date:

char sDate[] = "10/04/2014";



我必须得到它的双倍值 - 41739


and I have to get double value from it - "41739"

推荐答案

int calulate_day_number_since_19000101(int year, int month, int day)
{
    int days = 0;
    for (int y = 1900; y < year; ++y)
    {
        days += 365;
        if (is_leap_year(y)) days++;
    }
    for (int m = 1; m < month; ++m)
    {
        if (m == 2)
        {
            days += 28;
            if (is_leap_year(year)) days++;
        }
        else if (m < 8)
        {
            days += m % 2 ? 31 : 30;
        }
        else
        {
            days += m % 2 ? 30 : 31;
        }
    }
    days += day;
}
int is_leap_year(int year)
{
    if (year % 400  == 0) return 1;
    if (year % 100  == 0) return 0;
    if (year % 4    == 0) return 1;
    return 0;
}





calulate_day_number_since_19000101(1900,1,1); 返回1.

calulate_day_number_since_19000101(2014,4,10); 返回41738.



那么,为什么41739对我来说并不清楚...我猜你的参考实施的闰年计算是错误的:每100年不是闰年,但每400年再次是闰年。如果忽略上述事实,我也会得到41739.



干杯

Andi



PS:将字符串解析为年,月,日作为练习; - )



calulate_day_number_since_19000101(1900, 1, 1); returns 1.
calulate_day_number_since_19000101(2014, 4, 10); returns 41738.

So, why 41739 is not clear to me... I guess the leap year calculation of your reference implementation is wrong: every 100th year is not a leap year, but every 400th year is a leap year again. If ignoring the above mentioned facts, I also get 41739.

Cheers
Andi

PS: Parsing the string into year, month, day is left as exercise ;-)



这篇关于如何将带日期的char转换为double?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-24 08:48