{"title": "C++ 计算时间秒数和转换时间格式", "description": "本文介绍了使用 C++ 编程语言计算从今天凌晨零点开始过了多少秒,以及将从今天零点开始经历的秒数转换为标准格式的时分秒时间。提供了示例代码和输入输出示例。", "keywords": "C++, 时间计算, 秒数, 时分秒, 时间转换, 代码示例", "content": ""#include \n#include \n#include \n\nusing namespace std;\n\nint main() {\n string time;\n int seconds;\n \n // 输入当前的标准时间和经历的秒数\n getline(cin, time);\n cin >> seconds;\n \n // 将标准时间转换为秒数\n int hour = stoi(time.substr(0, 2));\n int minute = stoi(time.substr(3, 2));\n int second = stoi(time.substr(6, 2));\n int totalSeconds = hour * 3600 + minute * 60 + second;\n \n // 计算从零点开始过了多少秒\n int passedSeconds = totalSeconds + seconds;\n \n // 计算新的时分秒时间\n int newHour = passedSeconds / 3600;\n int newMinute = (passedSeconds % 3600) / 60;\n int newSecond = (passedSeconds % 3600) % 60;\n string newTime = to_string(newHour) + ":" + to_string(newMinute) + ":" + to_string(newSecond); \n \n // 输出结果\n cout << passedSeconds << endl;\n cout << newTime << endl;\n \n return 0;\n}\n"}

C++ 计算时间秒数和转换时间格式

原文地址: https://www.cveoy.top/t/topic/pLjY 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录