// Current date/time based on current system
time_t now = time(0);
// Convert now to tm struct for local timezone
tm* localtm = localtime(&now);
cout << "The local date and time is: " << asctime(localtm) << endl;
// Convert now to tm struct for UTC
tm* gmtm = gmtime(&now);
if (gmtm != NULL) {
cout << "The UTC date and time is: " << asctime(gmtm) << endl;
}
else {
cerr << "Failed to get the UTC date and time" << endl;
return EXIT_FAILURE;
}
#include "tz.h"
#include <iostream>
int
main()
{
using namespace date;
using namespace std::chrono;
auto local = make_zoned(current_zone(), system_clock::now());
std::cout << local << '\n';
}
#include <iostream>
#include <ctime>
using namespace std;
/**
* This function gets the current date time
* @param useLocalTime true if want to use local time, default to false (UTC)
* @return current datetime in the format of "YYYYMMDD HHMMSS"
*/
string getCurrentDateTime(bool useLocalTime) {
stringstream currentDateTime;
// current date/time based on current system
time_t ttNow = time(0);
tm * ptmNow;
if (useLocalTime)
ptmNow = localtime(&ttNow);
else
ptmNow = gmtime(&ttNow);
currentDateTime << 1900 + ptmNow->tm_year;
//month
if (ptmNow->tm_mon < 9)
//Fill in the leading 0 if less than 10
currentDateTime << "0" << 1 + ptmNow->tm_mon;
else
currentDateTime << (1 + ptmNow->tm_mon);
//day
if (ptmNow->tm_mday < 10)
currentDateTime << "0" << ptmNow->tm_mday << " ";
else
currentDateTime << ptmNow->tm_mday << " ";
//hour
if (ptmNow->tm_hour < 10)
currentDateTime << "0" << ptmNow->tm_hour;
else
currentDateTime << ptmNow->tm_hour;
//min
if (ptmNow->tm_min < 10)
currentDateTime << "0" << ptmNow->tm_min;
else
currentDateTime << ptmNow->tm_min;
//sec
if (ptmNow->tm_sec < 10)
currentDateTime << "0" << ptmNow->tm_sec;
else
currentDateTime << ptmNow->tm_sec;
return currentDateTime.str();
}
The current time is 11:43:41 am
The current date is 6-18-2015 June Wednesday
Day of month is 17 and the Month of year is 6,
also the day of year is 167 & our Weekday is 3.
The current year is 2015.
代码:
#include <ctime>
#include <iostream>
#include <string>
#include <stdio.h>
#include <time.h>
using namespace std;
const std::string currentTime() {
time_t now = time(0);
struct tm tstruct;
char buf[80];
tstruct = *localtime(&now);
strftime(buf, sizeof(buf), "%H:%M:%S %P", &tstruct);
return buf;
}
const std::string currentDate() {
time_t now = time(0);
struct tm tstruct;
char buf[80];
tstruct = *localtime(&now);
strftime(buf, sizeof(buf), "%B %A ", &tstruct);
return buf;
}
int main() {
cout << "\033[2J\033[1;1H";
std:cout << "The current time is " << currentTime() << std::endl;
time_t t = time(0); // get time now
struct tm * now = localtime( & t );
cout << "The current date is " << now->tm_mon + 1 << '-'
<< (now->tm_mday + 1) << '-'
<< (now->tm_year + 1900)
<< " " << currentDate() << endl;
cout << "Day of month is " << (now->tm_mday)
<< " and the Month of year is " << (now->tm_mon)+1 << "," << endl;
cout << "also the day of year is " << (now->tm_yday)
<< " & our Weekday is " << (now->tm_wday) << "." << endl;
cout << "The current year is " << (now->tm_year)+1900 << "."
<< endl;
return 0;
}
1
localtime_s()版本:
#include <stdio.h>
#include <time.h>
int main ()
{
time_t current_time;
struct tm local_time;
time ( ¤t_time );
localtime_s(&local_time, ¤t_time);
int Year = local_time.tm_year + 1900;
int Month = local_time.tm_mon + 1;
int Day = local_time.tm_mday;
int Hour = local_time.tm_hour;
int Min = local_time.tm_min;
int Sec = local_time.tm_sec;
return 0;
}
4
#include <Windows.h>
void main()
{
//Following is a structure to store date / time
SYSTEMTIME SystemTime, LocalTime;
//To get the local time
int loctime = GetLocalTime(&LocalTime);
//To get the system time
int systime = GetSystemTime(&SystemTime)
}
30
您可以使用以下代码以C语言获取当前系统日期和时间:
#include <iostream>
#include <time.h> //It may be #include <ctime> or any other header file depending upon
// compiler or IDE you're using
using namespace std;
int main() {
// current date/time based on current system
time_t now = time(0);
// convert now to string form
string dt = ctime(&now);
cout << "The local date and time is: " << dt << endl;
return 0;
}
22 回答
你可以使用boost:
在C 11中你可以使用std::chrono::system_clock::now()
C与C共享其日期/时间函数 . 对于C程序员来说,tm structure可能是最简单的 - 以下打印今天的日期:
您可以尝试以下跨平台代码来获取当前日期/时间:
输出:
有关日期/时间格式的更多信息,请访问here
std C库提供time() . 这是纪元的秒数,可以使用标准C函数转换为日期和
H:M:S
. Boost 也有a time/date library,你可以检查 .C标准库未提供正确的日期类型 . C从C继承了日期和时间操作的结构和函数,以及考虑本地化的几个日期/时间输入和输出函数 .
旧问题的新答案:
问题没有说明在什么时区 . 有两种合理的可能性:
在UTC中 .
在计算机的本地时区 .
对于1,您可以使用this date library和以下程序:
这只是输出给我:
日期库基本上只是为
std::chrono::system_clock::time_point
添加了一个流操作符 . 它还添加了许多其他不错的功能,但这并没有在这个简单的程序中使用 .如果你更喜欢2(当地时间),那么timezone library就 Build 在date library之上 . 假设编译器支持C 11或C 14,这两个库都是 open source 和 cross platform .
这对我来说只输出:
make_zoned
的结果类型是date::zoned_time
,它是date::time_zone
和std::chrono::system_clock::time_point
的配对 . 此对代表本地时间,但也可以表示UTC,具体取决于您查询它的方式 .通过上面的输出,您可以看到我的计算机当前处于UTC偏移为-4h的时区,并且缩写为EDT .
如果需要其他时区,也可以完成 . 例如,要查找悉尼当前时间,澳大利亚只需将变量
local
的构造更改为:输出变为:
(对于谷歌同行)
还有Boost::date_time:
是的,您可以使用当前使用的区域设置指定的格式规则来执行此操作:
使用std::time()或std::chrono::system_clock::now()(或其他clock type)获取当前时间 .
std::put_time()(C 11)和strftime()(C)提供了大量格式化程序来输出这些时间 .
格式化程序的顺序很重要:
strftime()
的格式化程序类似:通常,大写格式化程序意味着“完整版本”,小写字母意味着缩写(例如Y:2017,y:17) .
区域设置会改变输出:
可能的输出(Coliru,Compiler Explorer):
我已经使用std::gmtime()转换为UTC . std::localtime()用于转换为本地时间 .
请注意,其他答案中提到的asctime() / ctime()现已标记为已弃用,应优先使用
strftime()
.你可以使用C 11时间班:
出局:
始终有
__TIMESTAMP__
预处理器宏 .例如:2014年4月13日星期日11:28:08
您也可以直接使用
ctime()
:我发现此链接对我的实现非常有用:C++ Date and Time
这是我在实现中使用的代码,以获得明确的“YYYYMMDD HHMMSS”输出格式 . 该参数用于在UTC和本地时间之间切换 . 您可以轻松修改我的代码以满足您的需求 .
输出(UTC,EST):
这是针对我在Linux(RHEL)和Windows(x64)上针对g和OpenMP编译的:
ffead-cpp为各种任务提供了多个实用程序类,其中一个类是Date类,它提供了从Date操作到日期算术的许多功能,还提供了一个用于计时操作的Timer类 . 你可以看一下 .
http://www.cplusplus.com/reference/ctime/strftime/
这种内置似乎提供了一套合理的选择 .
这适用于G我不确定这是否对您有所帮助 . 节目输出:
代码:
localtime_s()版本:
您可以使用以下代码以C语言获取当前系统日期和时间:
PS:Visit此网站了解更多信息 .