3
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

[BluePrism]日付として入ってきた文字列が、カレンダーに存在する正しい日付かどうかを判定する部品を作りました。

Posted at

作ったもの

有効な日付かどうかを判定するものをつくりました。
計算ステージでIsDate関数を使ったら、区切り文字がないものが入ってきたときに判定してもらえなかったので。

  • 年は2文字もしくは4文字
  • 区切り文字がある場合は月と日は1文字でもオッケー
  • 区切り文字は無しもしくは「/」「-」「.」のいずれか

上記条件を満たすときのみカレンダーに存在する日付かどうかを見ます。
それ以外のフォーマットは全部日付じゃないとします。(なので2019.1とか6/12とかはダメです)
yyyyMMdd yyyy/MM/dd yyyy-MM-dd yyyy.MM.dd
yyMMdd yy/MM/dd yy-MM-dd yy.MM.dd

作りかた

コードステージに以下のように書きます。

入力:InDate(型はText) 半角でお願いします
出力:IsDate(型はFlag)

DateTime tmp;

if (System.Text.RegularExpressions.Regex.IsMatch(InDate,
    @"^(?<year>[0-9]{4}|[0-9]{2})(?<datesep>\/|-|\.|)" + 
    @"(?<month>0?[1-9]|1[012])\k<datesep>" + 
    @"(?<day>0?[1-9]|[12][0-9]|3[01])$"))
{
	if (InDate.IndexOf("/") < 0 && InDate.IndexOf("-") < 0 && InDate.IndexOf(".") < 0)
	{
		if(InDate.Length == 8)
		InDate = InDate.Substring( 0, 4 ) + "/" + InDate.Substring( 4, 2 ) + "/" + InDate.Substring( 6, 2 );
		else if(InDate.Length == 6)
		InDate = InDate.Substring( 0, 2 ) + "/" + InDate.Substring( 2, 2 ) + "/" + InDate.Substring( 4, 2 );
	}
	IsDate = DateTime.TryParse(InDate, out tmp);
}
else
{
	IsDate = false;
}


終わりです

c#とか読んだことも書いたこともなく……
とりあえず見よう見まねで書いて、動いたからたぶんこれで合ってるのかなーというレベルです。
違うところとかがあったら教えてください。ありがとうございます。

3
3
2

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?