在 c++ 中读取日期中的月份部分
Reading the month part in the date in c++
我有两个日期,即文件创建日期和系统日期,均为 int 格式
"20120304","20120404" which is something like YYYYMMDD.
我只想将这两个日期的 MM 部分作为整数从函数返回。
请提出一些例子
谢谢
因为它是int
类型使用int mm=(yyyymmdd/100)%100
虽然我更喜欢@keety的答案,但还有另一种方法。您可以使用字符串流:
例如:
#include <sstream>
int i = 5;
std::string s;
std::stringstream out;
out << i;
s = out.str();
因此,在您将字符串作为字符串变量中的YYYYMMDD
之后。 substr(4,2)
将是你的月份。您可以稍后使用 strtol
转换回整数。
如果它们总是采用 YYYYMMDD
格式,则取 [4] 处的子字符串作为长度 2。
相关文章:
- 查询SQLite数据库中的日期
- 获取日期异步信号安全吗?如果在信号处理程序中使用,它会导致死锁吗
- Log4cpp:以UTC/GMT时区打印日期
- 如何打开并写入一个名称取自C++中字符串的文件
- 使用C++的日期库读取时间
- 如何将部分流作为参数传递
- 日期格式为C++
- 如何在 C++20 计时中为日期添加天数?
- 如何在QTableView中排序和更改日期格式
- 当我尝试在 Main 中调用插入函数时,它不是取数字?
- 如何在cpp中使用地图显示给定日期范围内(在下面的问题中)的费率?
- Gtkmm 会不断检查 Gtk::日历上的所选日期是否发生变化
- scanf() 只取数字作为输入,避免使用其他字符
- 我可以通过取每个数字的模并取和来计算大数的模数吗?
- 有没有办法修改'operator->',以便'z->im'返回复数的虚部
- fmt 与 Howard Hinnant 的日期:为什么从 fmt::to_string "{}"?FMT 和日期的最佳实践?
- 使程序检查当前日期是否=预期日期
- 为什么负数不取模向量大小给出负数?
- 如何找到两个日期之间的时间差异(以秒和纳秒为单位)?
- JNI 日期值转换问题,在C++中获取不同的长整型值