[C] C语言中获得系统当前日期 和 时间

获得系统当前的日期和时间

1. 

#include 
#include 
int main()
{
    time_t timep;
    struct tm *p;
    time(&timep);
    p =localltime(&timep); //此函数获得的tm结构体的时间,是已经进行过时区转化为本地时间
    //p = gmtime(&timep); //把日期和时间转换为格林威治(GMT)时间的函数

    printf("Year:  %d\n", 1900+p->tm_year);
    printf("Month:  %d\n", 1+p->tm_mon);
    printf("Day:  %d\n", p->tm_mday);
    printf("Hour:  %d\n", p->tm_hour);
    printf("Minute:  %d\n", p->tm_min);
    printf("Second:  %d\n",  p->tm_sec);
    printf("Weekday:  %d\n", p->tm_wday);
    printf("Days:  %d\n", p->tm_yday);
    printf("Isdst:  %d\n", p->tm_isdst);
}


2. 

/*
 * =====================================================================================
 *
 *       Filename:  Time04.c
 *
 *    Description:  输入格式化后的系统当前时间
 *
 *        Version:  1.0
 *        Created:  2012年07月10日 22时34分31秒
 *       Revision:  none
 *       Compiler:  gcc
 *
 *         Author:  ShanHaiyang 
 *   Organization:  
 *
 * =====================================================================================
 */
#include 
#include 
#include 
#include 

int main()
{
    time_t timep;
    char s[30];
    
    time(&timep);

    strcpy(s,ctime(&timep));

    printf("%s\n", s);
}


你可能感兴趣的:(C)