C++中时间相关函数的使用

结构类型的字段 tm 存储下面的值,其中每个为 int。

struct tm {
  int tm_sec;   // 秒,正常范围从 0 到 59,但允许至 61
  int tm_min;   // 分,范围从 0 到 59
  int tm_hour;  // 小时,范围从 0 到 23
  int tm_mday;  // 一月中的第几天,范围从 1 到 31
  int tm_mon;   // 月,范围从 0 到 11
  int tm_year;  // 自 1900 年起的年数
  int tm_wday;  // 一周中的第几天,范围从 0 到 6,从星期日算起
  int tm_yday;  // 一年中的第几天,范围从 0 到 365,从 1 月 1 日算起
  int tm_isdst; // 夏令时
}
#include "stdafx.h"
#include <string.h>
#include <iostream>
#include <time.h>
#include <iomanip>
using std::setw;
using namespace std;
int main(void)
{
    struct tm t;   //tm结构指针
    time_t now= time(0);;  //声明time_t类型变量
    time(&now);      //获取系统日期和时间
    localtime_s(&t, &now);   //获取当地日期和时间
    //格式化输出本地时间
    cout << "年:" << setw(13) << t.tm_year +1900 << endl;
    cout << "月:" << setw(13) << t.tm_mon + 1 << endl;
    cout << "日:" << setw(13) << t.tm_mday << endl;
    cout << "周:" << setw(13) << t.tm_wday << endl;
    cout << "一年中:" << setw(13) << t.tm_yday << endl;
    cout << "时:" << setw(13) << t.tm_hour << endl;
    cout << "分:" << setw(13) << t.tm_min << endl;
    cout << "秒:" << setw(13) << t.tm_sec << endl;
    cout << "夏令时:" << setw(13) << t.tm_isdst << endl;

    //将当前时间转化成字符串形式
    char str[26];
    ctime_s(str, sizeof str, &now);
    cout << "本地日期和时间:" << str << endl;


    return 0;

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值