次の書式のいずれか(同じプログラム内のすべてを処理する必要があります)に書かれた日付を指定すると、有効な
yyyy/mm/dd日付
に解析されます。
17th May 2012
March 14th, 2016
20 February 2014
September 14, 2017
Sunday, June 8, 2015
ルール
- Dates will sometimes be invalid, ie. incorrect day for the
month or number of months in a year, you must handle both cases.
Either by erroring out or returning a consistent falsey value, you
choose. (They will however stick to the template formats
above) - Padding for days and months less than 10 must be used to create
a two digit output. - Month names will always be the full name, not shortened to
their three character counterparts. - You can assume the year will always be within the 0000-9999
range. - Negative numbers need not be handled.
- You can create a full program or function so output can be in
any format, printed to console or returned from a function. - Input will always be a string, output must always be a string,
if it makes it shorter to take it as a single argument in an array
eg.["17th May 2012"]
you may do so and output can be
the same["2012/05/17"]
- You can assume spelling in input will be correct.
BONUS: cos who here doesnt like a challenge
😉
2016年3月14日
または
2016年14月
の入力形式を許可することができる場合は、 20バイト
>最後のバイト数が1未満の場合は1になります。
スペルの混乱を避けるために、それぞれの日の完全な数字がここにあります。
First, Second, Third, Fourth, Fifth, Sixth, Seventh, Eighth, Nineth, Tenth, Eleventh, Twelfth, Thirteenth, Fourteenth, Fifteenth, Sixteenth, Seventeenth, Eighteenth, Nineteenth, Twentieth, Twenty First, Twenty Second, Twenty Third, Twenty Fourth, Twenty Fifth, Twenty Sixth, Twenty Seventh, Twenty Eighth, Twenty Nineth, Thirtieth, Thirty First
テストケース
INPUT | Output
17th May 2012 | 2012/05/17
March 14th, 2016 | 2016/03/14
20 February 2014 | 2014/02/20
September 14, 2017 | 2017/09/14
Sunday, June 8, 2015 | 2015/06/08
1st January 1918 | 1918/01/01
The Fourteenth of March, 2016 | 2016/03/14
March the Fourteenth, 2016 | 2016/03/14
November the Seventeenth, 2019 | 2019/11/17
The Thirtieth of April, 2016 | 2016/04/30
30 February 2014 | Invalid
September 99, 2017 | Invalid
Sunday, June8, 2015 | Invalid
The Thirty First of April, 2016 | Invalid
ベストアンサー
PowerShell, 91 89 91
56 bytes
date("$args"-replace'th|rd|st|(b.*)day|,')-f yyyy/MM/dd
入力を文字列として受け取ります。 -replace
を使用して迷惑メールを取り除き、組み込みの
Get-Date
コマンドを -f
ormatフラグとともに使用して、必要な
yyyy/MM/dd
形式です。その文字列はパイプライン上に残され、出力は暗黙的です。
Mr Xcoderのおかげで2バイトを節約できました。
TessellatingHecklerの正規表現ゴルフのおかげで巨大なチャンクを保存しました。