输出时间和日期,格式为:2018-01-26 15:51.159753
Output time and date in the format: 2018-01-26 15:51.159753
我正在尝试找到以下格式输出当前日期和时间(毫秒(的解决方案: 2018-01-26 15:51:02.159753
我现在拥有的(工作(如下:
#include <iostream>
#include <ctime>
using namespace std;
int main() {
// current date/time based on current system
time_t now = time(0);
// convert now to string form
char* dt = ctime(&now);
cout << "The local date and time is: " << dt << endl;
// convert now to tm struct for UTC
tm *gmtm = gmtime(&now);
dt = asctime(gmtm);
cout << "The UTC date and time is:"<< dt << endl;
}
虽然我没有找到任何方法来重新制作它,以便以我想要的格式输出日期和时间。任何这方面的帮助将不胜感激。
所以你需要使用localtime
中的tm
,它可能应该像这样初始化:
const auto buz = chrono::system_clock::now().time_since_epoch();
const auto baz = chrono::floor<chrono::seconds>(buz);
const auto bar = chrono::system_clock::to_time_t(chrono::system_clock::time_point(baz));
const auto foo = localtime(&bar);
然后,您可以将其与put_time
一起使用:
cout << put_time(foo, "%F %T.") << (buz - baz).count() << endl;
现场示例
相关文章:
- C++错误:初始值设定项过多,无法'char[26]'
- 将"-01"替换为"-02" 英特尔编译器选项会导致 FPE 在较小的 for 循环行程计数中抛出
- 我的程序将 26 个字母转换为 ascII 没有显示正确答案
- 传递指针数组以在 Vivado HLS (SDSoC 2018) 中发挥作用
- PICorv32 RISC-V在Vivado 2018.2中的实现
- Unity: DllNotFoundException (Unity 2018.2; Android)
- 如何定义int[][26]类型的变量
- C/C++intellisense 0.26.1不显示结构成员列表
- 谷歌启动2018,测试用例中的整数错误
- 在2018年使用C++处理Unicode的正确方法是什么?
- 解决谷歌启动2018年第三季度的启动问题
- 'const char [26]' 和 'LPSTR ' 类型的无效操作数
- 父类有 26 个构造函数重载.如何在不复制+粘贴 26 个重载的情况下将一个小任务附加到所有构造器?
- TFS 2018 生成失败:无法打开包含文件"afxres.h"
- 此方案需要使用地图城镇 01
- 如何读取和处理格式化的日期,如 01/11/1998、星期日(10000 个输入)C++
- 循环以使用设置模板类将所有 26 个字母添加到 s 中
- 输出时间和日期,格式为:2018-01-26 15:51.159753
- 麻烦排序谷歌代码Jam 2018资格赛
- 如何使 2 = .01 和 .01 = 2