1、使用COleDateTime和COleDateTimeSpan类获取当月天数

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
int GetThisMonthDays()
{
COleDateTime time, nextMonth;
SYSTEMTIME stLocal;
GetLocalTime(&stLocal);
time.GetAsSystemTime(stLocal);
time.SetDateTime(time.GetYear(), time.GetMonth(), 1, 0, 0, 0);
if (time.GetMonth() >= 12)
{
nextMonth.SetDateTime(time.GetYear() + 1, 1, 1, 0, 0, 0);
}
else
{
nextMonth.SetDateTime(time.GetYear(), time.GetMonth() + 1, 1, 0, 0, 0);
}
COleDateTimeSpan ts = nextMonth - time;
return ts.GetDays();
}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
// 取某年某月的天数
int GetMonthDays(int year, int month)
{
if (month > 12 || month < 1)
{
return -1;
}
COleDateTime time, nextMonth;
time.SetDateTime(year, month, 1, 0, 0, 0);

if (time.GetMonth() >= 12)
{
nextMonth.SetDateTime(time.GetYear() + 1, 1, 1, 0, 0, 0);
}
else
{
nextMonth.SetDateTime(time.GetYear(), time.GetMonth() + 1, 1, 0, 0, 0);
}
COleDateTimeSpan ts = nextMonth - time;
return ts.GetDays();
}

2、获取时区

1
2
3
4
5
6
7
8
9
int GetTimeZone()
{
SYSTEMTIME t1, t2;
GetLocalTime(&t1);
GetSystemTime(&t2);
int n = t1.wHour - t2.wHour;
return n;
}

3、当时时间

1
2
SYSTEMTIME stLocal;
GetLocalTime(&stLocal);

4、系统时间

1
2
SYSTEMTIME stSys;
GetSystemTime(&stSys);