faustf Posted December 2, 2016 Posted December 2, 2016 hi guys i see exist a _DateToMonth if i have a number of month , the function answer me with , name of the month , but exist contrary ??? if i have a name of month the function return me a numebr? i saw help but i not see nothing similar thankz at all
kylomas Posted December 2, 2016 Posted December 2, 2016 (edited) faustf, Roll your own... Local $mon = MonthtoNumber('jan') ConsoleWrite((@error ? '! Error >>> ' & @error : $mon) & @CRLF) Func MonthtoNumber($str) If Not StringIsAlpha($str) Then Return SetError(1, 0, $str) If StringLen($str) < 3 Then Return SetError(2, 0, $str) Local $aMonths = ['january', 'february', 'march', 'april', 'may', 'june', 'july', 'august', 'september', 'october', 'november', 'december'] For $i = 0 To UBound($aMonths) - 1 If StringInStr($aMonths[$i], $str) Then Return $i + 1 Next Return SetError(3, 0, $str) EndFunc ;==>MonthtoNumber kylomas Edited December 2, 2016 by kylomas added check for len of arg faustf 1 Forum Rules Procedure for posting code "I like pigs. Dogs look up to us. Cats look down on us. Pigs treat us as equals." - Sir Winston Churchill
faustf Posted December 2, 2016 Author Posted December 2, 2016 (edited) i did do a little modify Func _MonthToNumber($str) If Not StringIsAlpha($str) Then Return SetError(1, 0, $str) If StringLen($str) < 3 Then Return SetError(2, 0, $str) Local $aMonths = ['january', 'february', 'march', 'april', 'may', 'june', 'july', 'august', 'september', 'october', 'november', 'december'] For $i = 0 To UBound($aMonths) - 1 If StringInStr($aMonths[$i], $str) Then If StringLen($i) = 1 Then Return "0" & $i + 1 Else Return $i + 1 EndIf EndIf Next Return SetError(3, 0, $str) EndFunc ;==>_MonthToNumber i think now is perfect we can insert also in Date.au3 Edited December 2, 2016 by faustf
SadBunny Posted December 2, 2016 Posted December 2, 2016 (edited) Note that that StringInStr approach is potentially problematic as, for instance, "uar" is an ambiguous substring. Fringe, I know, but I'd suggest just matching it with StringLeft($str, 3) Also, you'd probably want it case-insensitive. Thirdly, you may not want to return the plain parameter in case of error in case someone forgets to check for @error. Lastly, you can oneline that zero-prefix thing. Suggestion: ConsoleWrite(_MonthToNumber("toolong") & @CRLF) ConsoleWrite(_MonthToNumber("", False) & @CRLF) ConsoleWrite(_MonthToNumber("uar", False) & @CRLF) ConsoleWrite(_MonthToNumber("feb", False) & @CRLF) ConsoleWrite(_MonthToNumber("DECeMBeR") & @CRLF) Func _MonthToNumber($str, $is1Based = True) If Not StringIsAlpha($str) Then Return SetError(1, 0, Null) If StringLen($str) < 3 Then Return SetError(2, 0, Null) Local $aMonths = ['january', 'february', 'march', 'april', 'may', 'june', 'july', 'august', 'september', 'october', 'november', 'december'] For $i = 0 To UBound($aMonths) - 1 If StringRegExp(StringLower($aMonths[$i]), StringLower("^" & $str)) Then Return StringFormat("%0.2i", $i + ($is1Based ? 1 : 0)) Next Return SetError(3, 0, Null) EndFunc ;==>_MonthToNumber Edited December 2, 2016 by SadBunny added 1-based months option Roses are FF0000, violets are 0000FF... All my base are belong to you.
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now