概述本文演示环境: Windows10使用C语言获取年月日时分秒毫秒,代码#include iostream#include string#include time.h#include sys/timeb.husing namespace std;struct NowDate{char year_month_day_...

概述
- 本文演示环境: Windows10
- 使用C语言获取年月日时分秒毫秒,
代码
#include <iostream>
#include <string>
#include <time.h>
#include <sys/timeb.h>
using namespace std;
struct NowDate
{
char year_month_day_[16] = {0}; //年月日
char hour_minute_second_[16] = {0}; //时分秒
char mill_sec_[4] = {0}; //毫秒
};
/// 获取时间
NowDate getTime()
{
time_t timep;
time(&timep);
NowDate date;
strftime(date.year_month_day_, sizeof(date.year_month_day_), "%Y-%m-%d", localtime(&timep));
strftime(date.hour_minute_second_, sizeof(date.hour_minute_second_), "%H:%M:%S", localtime(&timep));
struct timeb tb;
ftime(&tb);
sprintf(date.mill_sec_, "%d", tb.millitm);
return date;
}
int main()
{
NowDate time = getTime();
cout << "year-month-day:" << time.year_month_day_ << endl;
cout << "hour-minute-second" << time.hour_minute_second_ << endl;
cout << "mill-seconds" << time.mill_sec_ << endl;
system("pause");
return 0;
}
输出
沃梦达教程
本文标题为:C语言获取字符年月日时分秒毫秒


基础教程推荐
猜你喜欢
- C语言 structural body结构体详解用法 2022-12-06
- C++使用easyX库实现三星环绕效果流程详解 2023-06-26
- 一文带你了解C++中的字符替换方法 2023-07-20
- C语言基础全局变量与局部变量教程详解 2022-12-31
- 如何C++使用模板特化功能 2023-03-05
- C++中的atoi 函数简介 2023-01-05
- C利用语言实现数据结构之队列 2022-11-22
- 详解c# Emit技术 2023-03-25
- C/C++编程中const的使用详解 2023-03-26
- C++详细实现完整图书管理功能 2023-04-04