我想在取款,存款的时候给用户提供一个打印票据凭证的功能,但是需要获取本地时间,不知道如何才能实现,一点思路都没有😭
有一个头文件叫time.h,它可以管理时间。
它定义了一个Class,叫time_t,我们先声明一个
time_t now;
可现在还没有定义,有一个叫time()的函数,它可以先C语言的scanf一样定义你的变量
time(&now); //要有取地址符&
可现在了类型是time_t不可以正常使用,ctime()函数可以帮你把他变成char*字符串,同样,也要写地址符&
这里是一个实例:
#include <iostream>
#include <time.h>
using namespace std;
int main(){
time_t now;
time(&now);
char* str=ctime(&now);
cout<<str;
}
编译结果:
Sun Mar 19 22:51:28 2023
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main()
{
time_t cur_time; //定义变量
time(&cur_time); //获取当前时间
printf("%s",ctime(&cur_time)); //将当前时间转换为字符串并输出
system("pause");
return 0;
}