c语言如何输出当前的日期和时间?

 我来答
匿名用户
2017-08-11
展开全部
#include <stdio.h>
#include <time.h>
int main()
{
    time_t t;    //time_t是一种类型,定义time_t类型的t
    time(&t);    //取得当前时间
    printf("%s\n",ctime(&t));// ctime(&t)将日期转为字符串并打印
    return 0;
}
维真52
2017-08-14 · TA获得超过280个赞
知道答主
回答量:156
采纳率:100%
帮助的人:37.7万
展开全部
#include <time.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/timeb.h>
#include <string.h>

void main()
{
char tmpbuf[128], ampm[] = "AM";
time_t ltime;
struct _timeb tstruct;
struct tm *today, *gmt, xmas = { 0, 0, 12, 25, 11, 93 };

/* Set time zone from TZ environment variable. If TZ is not set,
* the operating system is queried to obtain the default value
* for the variable.
*/
_tzset();

/* Display operating system-style date and time. */
_strtime( tmpbuf );
printf( "OS time:\t\t\t\t%s\n", tmpbuf );
_strdate( tmpbuf );
printf( "OS date:\t\t\t\t%s\n", tmpbuf );

/* Get UNIX-style time and display as number and string. */
time( <ime );
printf( "Time in seconds since UTC 1/1/70:\t%ld\n", ltime );
printf( "UNIX time and date:\t\t\t%s", ctime( <ime ) );

/* Display UTC. */
gmt = gmtime( <ime );
printf( "Coordinated universal time:\t\t%s", asctime( gmt ) );

/* Convert to time structure and adjust for PM if necessary. */
today = localtime( <ime );
if( today->tm_hour > 12 )
{
strcpy( ampm, "PM" );
today->tm_hour -= 12;
}
if( today->tm_hour == 0 ) /* Adjust if midnight hour. */
today->tm_hour = 12;

/* Note how pointer addition is used to skip the first 11
* characters and printf is used to trim off terminating
* characters.
*/
printf( "12-hour time:\t\t\t\t%.8s %s\n",
asctime( today ) + 11, ampm );

/* Print additional time information. */
_ftime( &tstruct );
printf( "Plus milliseconds:\t\t\t%u\n", tstruct.millitm );
printf( "Zone difference in seconds from UTC:\t%u\n",
tstruct.timezone );
printf( "Time zone name:\t\t\t\t%s\n", _tzname[0] );
printf( "Daylight savings:\t\t\t%s\n",
tstruct.dstflag ? "YES" : "NO" );

/* Make time for noon on Christmas, 1993. */
if( mktime( &xmas ) != (time_t)-1 )
printf( "Christmas\t\t\t\t%s\n", asctime( &xmas ) );

/* Use time structure to build a customized time string. */
today = localtime( <ime );

/* Use strftime to build a customized time string. */
strftime( tmpbuf, 128,
"Today is %A, day %d of %B in the year %Y.\n", today );
printf( tmpbuf );
}

Output

OS time: 21:51:03
OS date: 05/03/94
Time in seconds since UTC 1/1/70: 768027063
UNIX time and date: Tue May 03 21:51:03 1994
Coordinated universal time: Wed May 04 04:51:03 1994
12-hour time: 09:51:03 PM
Plus milliseconds: 279
Zone difference in seconds from UTC: 480
Time zone name:
Daylight savings: YES
Christmas Sat Dec 25 12:00:00 1993

Today is Tuesday, day 03 of May in the year 1994.
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
匿名用户
2010-09-24
展开全部
//---------------------------------------------------------------------------

#include <stdio.h>
#include <time.h>

int main(void)
{
time_t tim;
struct tm *at;
char now[80];
time(&tim);
at=localtime(&tim);
strftime(now,79,"%Y-%m-%d\n%H:%M:%S\n",at);
puts(now);
return 0;
}
//---------------------------------------------------------------------------
本回答被提问者采纳
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
听不清啊
高粉答主

推荐于2017-08-14 · 说的都是干货,快来关注
知道顶级答主
回答量:7.8万
采纳率:89%
帮助的人:1.8亿
展开全部

本回答被网友采纳
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
匿名用户
2017-08-14
展开全部

已赞过 已踩过<
你对这个回答的评价是?
评论 收起
收起 更多回答(3)
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式