说明:获取当前的日期和时间(多种格式)
源码:
1 //获取多种格式的当前日期和时间(已封装函数)
2 #include <iostream>
3 #include <string>
4 #include <stdio.h>
5 #include <time.h>
6 using namespace std;
7 string Get_Date_And_Time(char *cType)
8 {
9 char cTimePARTNUMBER[256] = "";
10 time_t rawtime;
11 struct tm timeinfo;
12 time(&rawtime);
13 localtime_s(&timeinfo,&rawtime);
14 char buffer1[256];
15 strftime(cTimePARTNUMBER, sizeof(buffer1), cType, &timeinfo);//20180623
16 string strTimePARTNUMBER = "";
17 strTimePARTNUMBER = cTimePARTNUMBER;
18 return strTimePARTNUMBER;
19 //cType的值可以为:
20 //%Y%m%d 20190419
21 //%Y-%m-%d 2019-04-19
22 //%Y%m%d%H%M%S 20180623092858
23 //%a 缩写的星期几名称
24 //%A 完整的星期几名称 Sunday
25 //%b 缩写的月份名称 Mar
26 //%B 完整的月份名称 March
27 //%c 日期和时间表示法 Sun Aug 19 02:56:02 2012
28 //%d 一月中的第几天(01-31) 19
29 //%H 24 小时格式的小时(00-23) 14
30 //%I 12 小时格式的小时(01-12) 05
31 //%j 一年中的第几天(001-366) 231
32 //%m 十进制数表示的月份(01-12) 08
33 //%M 分(00-59) 55
34 //%S 秒(00-61) 02
35 //%U 一年中的第几周,以第一个星期日作为第一周的第一天(00-53) 33
36 //%w 十进制数表示的星期几,星期日表示为 0(0-6) 4
37 //%W 一年中的第几周,以第一个星期一作为第一周的第一天(00-53) 34
38 //%x 日期表示法 08/19/12
39 //%X 时间表示法 02:50:06
40 //%y 年份,最后两个数字(00-99) 01
41 //%Y 年份 2012
42 }
43 int main()
44 {
45 string strTestA = "";
46 strTestA = Get_Date_And_Time("%c"); //2019/11/20 17:39:23
47 cout << strTestA << endl;
48 strTestA = Get_Date_And_Time("%a"); //周三
49 cout << strTestA << endl;
50 strTestA = Get_Date_And_Time("%A"); //星期三
51 cout << strTestA << endl;
52 strTestA = Get_Date_And_Time("%Y%m%d"); //20191120
53 cout << strTestA << endl;
54 strTestA = Get_Date_And_Time("%Y-%m-%d"); //2019-11-20
55 cout << strTestA << endl;
56 strTestA = Get_Date_And_Time("%Y%m%d%H%M%S"); //20191120173923
57 cout << strTestA << endl;
58 strTestA = Get_Date_And_Time("%U");//第46周
59 cout << strTestA << endl;
60 system("pause");
61
62 return 0;
63 }
效果:
说明:获取几天后的日期和时间(多种格式)
源码:
1 //获得日期 iDay=0今天 iDay=-1昨天 iDay=1明天
2 string kk_get_date(char *cType,int iDay)
3 {
4 char cTimePARTNUMBER[256]="";
5 time_t rawtime;
6 struct tm * timeinfo;
7 //time ( &rawtime );
8 rawtime = time(NULL) + iDay * 24 * 3600;
9 timeinfo = localtime ( &rawtime );
10 char buffer1 [256];
11 strftime (cTimePARTNUMBER,sizeof(buffer1),cType,timeinfo);//20180623
12 string strTimePARTNUMBER="";
13 //list("%s\n",cTimePARTNUMBER);
14 strTimePARTNUMBER=cTimePARTNUMBER;
15 return strTimePARTNUMBER;
16 //20190419 "%Y%m%d"
17 //2019-04-19 "%Y-%m-%d"
18 //20180623092858 "%Y%m%d%H%M%S"
19 //%a 缩写的星期几名称
20 //%A 完整的星期几名称 Sunday
21 //%b 缩写的月份名称 Mar
22 //%B 完整的月份名称 March
23 //%c 日期和时间表示法 Sun Aug 19 02:56:02 2012
24 //%d 一月中的第几天(01-31) 19
25 //%H 24 小时格式的小时(00-23) 14
26 //%I 12 小时格式的小时(01-12) 05
27 //%j 一年中的第几天(001-366) 231
28 //%m 十进制数表示的月份(01-12) 08
29 //%M 分(00-59) 55
30 //%S 秒(00-61) 02
31 //%U 一年中的第几周,以第一个星期日作为第一周的第一天(00-53) 33
32 //%w 十进制数表示的星期几,星期日表示为 0(0-6) 4
33 //%W 一年中的第几周,以第一个星期一作为第一周的第一天(00-53) 34
34 //%x 日期表示法 08/19/12
35 //%X 时间表示法 02:50:06
36 //%y 年份,最后两个数字(00-99) 01
37 //%Y 年份 2012
38 }
说明:获取几天后的日期
源码:
1 //输入20200928
2 //输出 20200928 20200929 20200930 20201001
3 // 20201002 20201003 20201004
4 // 20201005 20201006 20201007
5 int get_next_10_days_str(string str8, vector< string > *vecTenDaysStr)
6 {
7 struct tm timeinfo;
8 timeinfo.tm_year = atoi(str8.substr(0, 4).c_str()) - 1900;
9 timeinfo.tm_mon = atoi(str8.substr(4, 2).c_str()) - 1;
10 timeinfo.tm_mday = atoi(str8.substr(6, 2).c_str());
11 timeinfo.tm_hour = 1;//1点
12 timeinfo.tm_min = 1;
13 timeinfo.tm_sec = 1;
14 time_t rawtime = mktime(& timeinfo);
15 for (int i = 0; i < 10; i++)
16 {
17 time_t rawtimeTemp = rawtime + i * 24 * 3600;
18 struct tm timeinfoTemp ;
19 localtime_s(&timeinfoTemp, &rawtimeTemp);
20 char cTimePARTNUMBER[256] = "";
21 strftime(cTimePARTNUMBER, 256, "%Y%m%d", &timeinfoTemp);//20180623
22 string strTemp = cTimePARTNUMBER;
23 (*vecTenDaysStr).push_back(strTemp);
24 }
25 return 0;
26 }
27
28 int main()
29 {
30 vector<string> vecTenDaysStr;
31 get_next_10_days_str("20200928", &vecTenDaysStr);
32 for (int i = 0; i < vecTenDaysStr.size(); i++)
33 {
34 cout << vecTenDaysStr[i] << endl;
35 }
36 cin.get();
37 return 0;
38 }
结果: