如果判断这个字符串代表一个有效的日期呢?

li3seyy 2009-05-19 09:05:26
一个表示日期的字符串,格式为:yyyy/mm/dd
那么如果判断这个字符串代表一个有效的日期呢?
如 2009/4/31,2009/2/29 为无效日期
而 2009/4/30,2009/2/28 为有效日期
...全文
198 14 打赏 收藏 转发到动态 举报
写回复
用AI写文章
14 条回复
切换为时间正序
请发表友善的回复…
发表回复
光宇广贞 2009-05-19
  • 打赏
  • 举报
回复
输入完了后。

首先检测字符串是否符合日期格式。

对日期判断:

一个switch 12 种情况加一个default

大小月不合的false

最后是闰年判断,不合的false

到default的,也就是非1-12者,必然false

其余的true
爪哇鹅 2009-05-19
  • 打赏
  • 举报
回复
加入闰年检测函数,返回一个布尔值,然后拆分这个字符串判断年份是否为闰年,如果是的话2月份就有29日,如果不是那么那个字符串就是错误的,对月份的检测也做一个函数。
wanghao111 2009-05-19
  • 打赏
  • 举报
回复
yrwx001 2009-05-19
  • 打赏
  • 举报
回复
#include "stdafx.h"
#include <stdio.h>
#include <string>
using namespace std;
bool IsValidDate(string sDate)
{
int yy;
int mm;
int dd;
yy = atoi(sDate.substr(0,4).c_str());
mm = atoi(sDate.substr(4,2).c_str());
dd = atoi(sDate.substr(6,2).c_str());
if (mm>=1 && mm <=12 && dd <=31 && dd >= 1)
{
if (yy%1000 ==0 && yy%400==0 && mm==2 && dd<=29)
return true;
else if (mm ==1 && mm == 3 && mm ==5 && mm==7 && mm==8 && mm==10 && mm==12 && dd==31)
return true;
else if (mm ==4 && mm == 6 && mm ==9 && mm==11 && dd<=30)
return true;
else if (mm ==2 && dd<=28)
return true;
else return false;
}
return false;
}

int main(int argc, char* argv[])
{
string sDate;
sDate = "20090227";
if (!IsValidDate(sDate))
printf("It is not a Valid Date!\n");
else
printf("It is a Valid Date!\n");
getchar();
return 0;
}
alan001 2009-05-19
  • 打赏
  • 举报
回复
楼上给出判断算法了
yrwx001 2009-05-19
  • 打赏
  • 举报
回复
#include "stdafx.h"
#include <stdio.h>
#include <string>
using namespace std;
bool IsValidDate(string sDate)
{
int yy;
int mm;
int dd;
yy = atoi(sDate.substr(1,4).c_str());
mm = atoi(sDate.substr(5,6).c_str());
dd = atoi(sDate.substr(7,8).c_str());
if (mm <=12 && dd <=31)
{
if (yy%1000 ==0 && yy%400==0 && mm==2 && dd==29) return true;
else if (mm ==1 && mm == 3 && mm ==5 && mm==7 && mm==8 && mm==10 && mm==12 && dd==31) return true;
else return false;
}
return false;
}

int main(int argc, char* argv[])
{
string sDate;
sDate = "20090229";
if (!IsValidDate(sDate))
printf("It is not a Valid Date!\n");
else
printf("It is a Valid Date!\n");
getchar();
return 0;
}
chenzhp 2009-05-19
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 wwoo_1105 的回复:]
写个函数判断卅,如果是硬性判断就用正则
[/Quote].
  • 打赏
  • 举报
回复
[Quote=引用楼主 li3seyy 的帖子:]
一个表示日期的字符串,格式为:yyyy/mm/dd
那么如果判断这个字符串代表一个有效的日期呢?
如 2009/4/31,2009/2/29 为无效日期
而 2009/4/30,2009/2/28 为有效日期
[/Quote]

首先sscanf(str,"%[^/]/%[^/]/%s",year,month,day);
解析出年月日。
然后你肯定要计算是否闰年。

就是判断y%4==0&&y%100!=0||y%400==0是否存在,
之后只是对month做switch,不在1到12的,break掉。

分别对1到12的判断day是否合格。

wwoo_1105 2009-05-19
  • 打赏
  • 举报
回复
写个函数判断卅,如果是硬性判断就用正则
lingyin55 2009-05-19
  • 打赏
  • 举报
回复
参考

bool panduan (int a,int b,int c)
{
if (b<=12&&c<=31)
{ if (a%1000==0&&a%400==0&&b=2&&c==29) return true;
else if ((b==1||b==3||b==5||b==7||b==8||b==10||b==12)&&c==31) return true;
else return false;
}
else return false;
}

/*在调用我这个函数的时候,应该这样调用:

bool d;
int x,y,z;
bool panduan (int,int,int);
cin>>x>>y>>z;
d=panduan (x,y,z);
if (d==true) cout<<"有效!\n";
else cout<<"无效!\n";

*/

//(别忘了加上:#include <iostream>)
jame2001 2009-05-19
  • 打赏
  • 举报
回复
StrToDateTime();

Call StrToDateTime to parse a string that specifies a date and time value. If S does not contain a valid date, StrToDateTime throws an EConvertError exception.

The S parameter must use the current locale's date/time format. In the US, this is commonly MM/DD/YY HH:MM:SS format. Specifying AM or PM as part of the time is optional, as are the seconds. Use 24-hour time (7:45 PM is entered as 19:45, for example) if AM or PM is not specified.
bitxinhai 2009-05-19
  • 打赏
  • 举报
回复
首先提取出年月日
然后根据年月日的关系判断是否合法(如大小月、闰月等)
lsupper 2009-05-19
  • 打赏
  • 举报
回复
简单点的话,或者我自己一直用得就是date -d "2009/2/29"....
system函数执行这个命令,判断返回值了

建议可以用正则表达式~~~

参考:

http://social.msdn.microsoft.com ... f-8aec-719e46ee2168
http://javascript.internet.com/forms/format-date.html
http://www.google.com/search?q=invalid+date+check+C


自己写个分割也可以~~~~好多~~~~~~~~

65,210

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧