如何将boost::date_time::date::day_of_week()转换为字符串类型
How to convert boost::date_time::date::day_of_week() to string type?
我想这样做。
ptime now = second_clock::local_time();
date today = now.date();
today.day_of_week();
string day = "Sat";
if(day == to_string(today.day_of_week()))
{
//perform an action
}
else
{
//perform another action
}
代码编译,但程序从不执行if块。我怎么能把day_of_week()转换成字符串?
我建议boost::lexical_cast<>
:
std::string day = boost::lexical_cast<std::string>(today.day_of_week());
或简单的:
std::cout << today.day_of_week();
Live On c++ Shell
#include <boost/date_time/posix_time/posix_time_io.hpp>
#include <boost/date_time/posix_time/posix_time.hpp>
#include <boost/lexical_cast.hpp>
int main() {
auto now = boost::posix_time::second_clock::local_time();
auto today = now.date();
std::string day = boost::lexical_cast<std::string>(today.day_of_week());
std::cout << today.day_of_week();
}
打印
Fri
相关文章:
- 防止主数据类型C++的隐式转换
- 模板参数替换失败,并且未完成隐式转换
- 努力将整数转换为链表。不知道我在这里做错了什么
- HEX值到wchar_t字符(UTF-8)的转换
- lambda参数转换为constexpr技巧,然后获取带链接的数组
- 将 Qvector<uint8_t> 转换为 QString
- 如何在cuSparse中使用cusparseXcoo2csr从coo转换为csc
- 有关插入适配器的错误。[错误]请求从 'back_insert_iterator<vector<>>' 类型转换为非标量类型
- 在c++中使用nlohmann从类到json的转换
- 从"int*"强制转换为"unsigned int"会丢失精度错误
- 将Integer转换为4字节的unsined字符矢量(按大端字节顺序)
- 使用 std::chrono / date::gps_clock 将双 GPS 时间戳转换为 UTC/tai
- 查找字符串中的 **date** 并将其转换为其数字形式
- C++不存在从"Date"到"Date (*)()"的合适转换函数
- 将2003年8月17日转换为boost::gregori::date
- 如何将std::字符串转换为boost::gregori::date
- 将日期std::字符串转换为QuantLib::date对象
- 如何将boost::date_time::date::day_of_week()转换为字符串类型
- 如何将boost::gregorian::date转换为mm/dd/yyyy格式,反之亦然
- 将Date转换为const char*