C/C++中关于时间的函数 ctime()

函数:ctime (const time_t * timer);

作用,将time_t型的时间转换为易读的string,转换后的string长度为25. 其格式为:

Www Mmm dd hh:mm:ss yyyy

但是有时只想提取其中的时间(hh:mm:ss),于是我写了下面一个小程序,用来提取其中的时间:

#include <stdio.h>
#include <time.h>
#include <string.h>
#define N 50
int main()
{
	time_t test;
	test = time(NULL);//抓取现在的时间 ——目前距离1970-01-01-00:00:00所经历的秒数 
	printf("now is %s\n",ctime(&test));//将上面的秒数转换为人们易读的时间表达方式 ,并在末尾换行 
	printf("length of ctime = %d\n",strlen(ctime(&test)));//ctime()所产生的字符串的长度 

	char c[N];//提取其中的时间 
	strcpy(c,ctime(&test));//先将其赋值给c 
	for(int i=11; i<=18;i++)//取第12位到第19位,注意,第一位为 i = 0; 
	printf("%c",c[i]);//打印时间 
	printf("\n");
	return 0;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值