Jump to content

questions about date


faustf
 Share

Recommended Posts

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

 

Link to comment
Share on other sites

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 by kylomas
added check for len of arg

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

Link to comment
Share on other sites

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  :D we can insert also  in  Date.au3 :D

 

Edited by faustf
Link to comment
Share on other sites

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 by SadBunny
added 1-based months option

Roses are FF0000, violets are 0000FF... All my base are belong to you.

Link to comment
Share on other sites

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 account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...