C语言日期时间函数

C语言 ctime()函数用于将把日期和时间转换为字符串。它是C语言的日期时间函数之一,位于标准库<time.h>中。

语法

语法如下:
char *ctime(const time_t *timer)

    参数

    • timer:time_t对象,它包含一个日历时间的指针。

    time_t在time.h文件中定义,用来表示时间数据类型,相当于是C语言long(长整形)类型的别名,是从1970年1月1日0时0分0秒到现在的秒数,时间戳。

    功能

    把日期和时间转换为字符串

    返回值

    返回时间的字符串格式,格式见如下例子。

    程序示例

    介绍一个例子,了解C语言 ctime()函数的使用方法。

    #include <stdio.h> 
    #include <time.h> 
    
    int main() 
    { 
    	time_t curtime;
            time(&curtime);//给time_t赋值
            printf("当前时间:%s", ctime(&curtime));
        
    	return 0;
    }
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

    程序运行结果:

    当前时间:Fri Jun  2 14:24:14 2023