当前位置:首页 » 编程语言 » c语言日期时间格式化年月日
扩展阅读
webinf下怎么引入js 2023-08-31 21:54:13
堡垒机怎么打开web 2023-08-31 21:54:11

c语言日期时间格式化年月日

发布时间: 2022-05-04 22:28:24

① c++设计一个函数,将年/月/日的格式表示的时间转换为年-月-日

#include<stdio.h>
// convert / to -:
char *ymd(char *s){
int i;
for (i=0;i<strlen(s);i++)if (s[i]=='/')s[i]='-';
return s;
}

main()
{
char y[]="2015/05/09";
printf("%s",ymd(y));
return 0;
}

c语言:从键盘上输入一个日期(格式:年月日),判断输入的日期是否正确,如果正确,计算该日期是这一年

package HXY;
import java.util.Scanner;
public class Calendar {
public static void main(String[] args) {
int year=0,month=0,date=0;
int temp=0;
int a[]={31,28,31,30,31,30,31,31,30,31,30,31};
Scanner sc=new Scanner(System.in);
System.out.println("请输入年份:");
year=sc.nextInt();
System.out.println("请输入月份:");
month=sc.nextInt();
System.out.println("请输入日期:");
date=sc.nextInt();
for (int i = 0; i < month-1; i++) {
temp +=a[i];
}
if((year%400==0||(year%4==0 &&year%100!=0))&&month>2){
System.out.println("这一天是今年的第"+(temp+1+date)+"天");
}else{
System.out.println("这一天是今年的第"+(temp+date)+"天");
}

}
运行结果如下:
请输入年份:
2015
请输入月份:
1
请输入日期:
1
这一天是今年的第1天

我这是用java语言妈的代码。谢谢。

③ 用C语言实现:首先获取系统时钟,然后时钟数据格式为:年月日时分秒(YYYYMMDDHHMMSS)

#include <stdio.h>

#include <time.h>

int main()
{
time_t rawtime;
struct tm * timeinfo;
time ( &rawtime );
timeinfo = localtime ( &rawtime );
int year,month,day,hour,min,sec;
year = 1900+timeinfo->tm_year;
month = 1+timeinfo->tm_mon;
day = timeinfo->tm_mday;
hour = timeinfo->tm_hour;
min = timeinfo->tm_min;
sec = timeinfo->tm_sec;
printf ( "当前时间:%4d-%02d-%02d %02d:%02d:%02d ",year, month,day,hour,min,sec);
printf ( "你需要的格式:%4d%02d%02d%02d%02d%02d ",year, month,day,hour,min,sec);

char hyy[2],lyy[2],MM[2],dd[2],hh[2],mm[2],ss[2];
sprintf(hyy,"%02X",year/100);
sprintf(lyy,"%02X",year%100);
sprintf(MM,"%02X",month);
sprintf(dd,"%02X",day);
sprintf(hh,"%02X",hour);
sprintf(mm,"%02X",min);
sprintf(ss,"%02X",sec);

printf("转化为16进制:%02s%02s%02s%02s%02s%02s%02s ",hyy,lyy,MM,dd,hh,mm,ss);
//exit(0);
return 0;
}

说明:我用VC6.0现写的,运行结果截图如下:

④ 怎么用c语言转换成年月日格式的求助啊。。。

#include<stdio.h>
intmain()
{
intyy,nn,dd,hh,mm,ss,len;
charout[2];
charinput[]="2015-06-2910:55:55pm";

len=sscanf(input,"%d-%d-%d%d:%d:%d%s",&yy,&nn,&dd,&hh,&mm,&ss,out);
printf("%d-%d-%d%d:%d:%d%slen-->%d",yy,nn,dd,hh,mm,ss,out,len);

return0;
}

⑤ C语言中怎么判断输入日期格式是否正确,要求输入的格式为年-月-日,如1995-12-06

char str[100];
memset(str,0,100);
strcpy(str,"1995-12-06")
int year,month,day;
sscanf(str,"%d-%d-%d",&year,&month,&day);//自己调试下就好了

⑥ C语言日期格式转换

1、c里没有相应的库,只能用asctime函数转换成一种固定格式。如果要转换,可以用sprintf把各种数据以“
1980-01-02
02:03:55

这种标准格式,格式到一个字符串中。
2、asctime函数:
原型:char*
asctime
(const
struct
tm
*
timeptr);
功能:把timeptr指向的tm结构体中储存的时间转换为字符串;
头文件:time.h;
返回值:一个固定格式的字符串。字符串格式为:www
mmm
dd
hh:mm:ss
yyyy。其中www为星期,mmm为月份,dd为日,hh为时,mm为分,ss为秒,yyyy为年份。
3、例程:
#include

#include

int main(){
time_t rawtime;
struct tm * timeinfo;
time(&rawtime);
timeinfo = localtime(&rawtime);//使用localtime函数把秒数时间rawtime转换为本地时间以tm结构体保存,并把tm结构体地址储存到timeinfo当中
printf("当前日期为: %s",asctime(timeinfo));//使用asctime函数把tm结构体中储存的时间转换为字符串,并输出
return 0;
}

⑦ c语言,如何进行日期格式转换

time.h 有函数 strftime 输出各种格式,但没有 你的 11th 13rd 格式。
简单办法是用查表法

#include "stdio.h"
#include "stdlib.h"
void main()
{
char dmy[20]="13/12/2010";
int i,j;
int a,b,c;
char d[32][5]={"0","1st","2nd","3rd","4th","5th","6th","7th","8th","9th","10th",
"11th","12th","13rd","14th","15th","16th","17th","18th",
"19th",.....,"31st"}; // 请自己补全
char m[13][4]={" ","Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"};
j = strlen(dmy);
printf("j=%d\n",j);
for (i=0;i<j ;i++) if ( dmy[i] =='/') dmy[i]=' ';
sscanf(dmy,"%d %d %d",&a,&b,&c);
printf("%s %s %d",d[a],m[b],c); // 打印出你要的 13rd Dec 2010

}

⑧ 求助!用c语言编程:按年月日输出系统日期按时分秒输出系统时间。要求包含结构体;高手谢谢了O(∩_∩)O~

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

struct MyTime
{
int year;
int month;
int day;
int hour;
int min;
int sec;

void set()
{

char buffer[10];
time_t tt = time( NULL);
struct tm * t = gmtime(&tt);

/*
size_t strftime( char *str, size_t maxsize, const char *fmt, struct tm *time );

功能:函数按照参数fmt所设定格式将time类型的参数格式化为日期时间信息,
然后存储在字符串str中(至多maxsize 个字符)。用于设定时间不同类型的代码为
*/

strftime(buffer,10,"%Y",t);
/*
atoi(char* str)

功能:将字符串str转换成一个整数并返回结果
*/
year = atoi(buffer);

strftime(buffer,10,"%m",t);
month = atoi(buffer);

strftime(buffer,10,"%d",t);
day = atoi(buffer);

strftime(buffer,10,"%H",t);
hour = atoi(buffer);

strftime(buffer,10,"%M",t);
min = atoi(buffer);

strftime(buffer,10,"%S",t);
sec = atoi(buffer);
}
int print()
{
set();
return printf("%4d-%.2d-%.2d %.2d:%.2d:%.2d",year,month,day,hour,min,sec);
}
};
int main(int argc, char *argv[])
{
struct MyTime t;
int printCharNum = 0;
int i;

while(1)
{
printCharNum = t.print();
// 清屏
for(i = 0 ; i < printCharNum ; i ++)
{
printf("\b");
}
}
return 0;
}

⑨ C语言中如何把月日年输出为年月日

这要看原来的程序中,月日年是怎么组织的?如果分别存放在三个变量中,那么只要把这三个变量的输出顺序交换一下就可以了。
例如,
int y,m,dm,d;
printf("请输入月 日 年:");
scanf("%d%d%d",&m,&d,&y);
printf("%d年%d月%d日",y,m,d);