DatetimeUtils
小于 1 分钟cpp
概述
自己制作的日期时间工具
源码
// 日期时间函数用到
#include <stdio.h>
#include <time.h>
#include <sstream>
#include <iostream>
using namespace std;
string getNowStr() {
struct tm t; //tm结构指针
time_t now; //声明time_t类型变量
time(&now); //获取系统日期和时间
localtime_s(&t, &now); //获取当地日期和时间
//格式化输出本地时间
int year = t.tm_year + 1900;
int month = t.tm_mon + 1;
/*printf("年:%d\n", year);
printf("月:%d\n", month);
printf("日:%d\n", t.tm_mday);
printf("周:%d\n", t.tm_wday);
printf("一年中:%d\n", t.tm_yday);
printf("时:%d\n", t.tm_hour);
printf("分:%d\n", t.tm_min);
printf("秒:%d\n", t.tm_sec);
printf("夏令时:%d\n", t.tm_isdst);*/
ostringstream oss;
// 将数字和字符拼接起来
oss << year << "年" << month << "月" << t.tm_mday << "日" << " " << t.tm_hour << ":" << t.tm_min << ":" << t.tm_sec;
return oss.str();
}
使用案例
// 在 cpp 文件头部声明函数原型
string getNowStr();
// 代码中使用
cout << "now:" << getNowStr() << endl;
