Jump to content

name of month udf


steveR
 Share

Recommended Posts

I needed this but didn't find anything in a search.

Returns the name of the current month in long or short format.

Please tell me if there are any problems.

;
;===============================================================================
;
; Function name: _MonName()
; Description: Return name of current month in short or long format
; Parameter(s): $format [optional] - 0 = long name (default); 1 = short name
; Return Value(s):  On Success - name of month; On error - sets @ERROR = 1, returns empty string
;
;===============================================================================
Func _MonName($format = 0)
    If Not StringIsInt($format) Or $format < 0 Or $format > 1 Then
        SetError(1)
        Return ""
    EndIf
    Local $monName
    If @MON = 1 Then $monName = "January"
    If @MON = 2 Then $monName = "February"
    If @MON = 3 Then $monName = "March"
    If @MON = 4 Then $monName = "April"
    If @MON = 5 Then $monName = "May"
    If @MON = 6 Then $monName = "June"
    If @MON = 7 Then $monName = "July"
    If @MON = 8 Then $monName = "August"
    If @MON = 9 Then $monName = "September"
    If @MON = 10 Then $monName = "October"
    If @MON = 11 Then $monName = "November"
    If @MON = 12 Then $monName = "December"
    If $format = 0 Then
        Return $monName
    ElseIf $format = 1 Then
        Return StringLeft($monName, 3)
    Else
        SetError(1)
        Return ""
    EndIf
EndFunc  ;==>_MonName
;

EDIT: Ran Tidy

Edited by steveR
AutoIt3 online docs Use it... Know it... Live it...MSDN libraryglobal Help and SupportWindows: Just another pane in the glass.
Link to comment
Share on other sites

Run Tidy on that and you got yourself a nice UDF :lmao:

"I thoroughly disapprove of duels. If a man should challenge me, I would take him kindly and forgivingly by the hand and lead him to a quiet place and kill him." - Mark TwainPatient: "It hurts when I do $var_"Doctor: "Don't do $var_" - Lar.
Link to comment
Share on other sites

An alternative (though I didn't comment it):

Func _MonthName($format = 0)
    If $format = 0 Then
        Local $months = StringSplit("January,February,March,April,May,June,July,August,September,October,November,December", ",")
        Return $months[@MON]
    ElseIf $format = 1 Then
        $abbrev = StringSplit("Jan,Feb,Mar,Apr,June,July,Aug,Sept,Oct,Nov,Dec", ",")
        Return $abbrev[@MON] & "."
    Else
        SetError(1)
        Return ""
    EndIf
EndFunc

EDIT: As pointed out below this code has a bug!

Edited by CyberSlug
Use Mozilla | Take a look at My Disorganized AutoIt stuff | Very very old: AutoBuilder 11 Jan 2005 prototype I need to update my sig!
Link to comment
Share on other sites

i made one once, i did it just like cybersulg :lmao:

[font="Times"] If anyone remembers me, I am back. Maybe to stay, maybe not.----------------------------------------------------------------------------------------------------------[/font][font="Times"]Things I am proud of: Pong! in AutoIt | SearchbarMy website: F.R.I.E.S.A little website that is trying to get started: http://thepiratelounge.net/ (not mine)[/font][font="Times"] ----------------------------------------------------------------------------------------------------------[/font][font="Arial"]The newbies need to stop stealing avatars!!! It is confusing!![/font]

Link to comment
Share on other sites

I shall find that bug!

[font="Times"] If anyone remembers me, I am back. Maybe to stay, maybe not.----------------------------------------------------------------------------------------------------------[/font][font="Times"]Things I am proud of: Pong! in AutoIt | SearchbarMy website: F.R.I.E.S.A little website that is trying to get started: http://thepiratelounge.net/ (not mine)[/font][font="Times"] ----------------------------------------------------------------------------------------------------------[/font][font="Arial"]The newbies need to stop stealing avatars!!! It is confusing!![/font]

Link to comment
Share on other sites

It's "Shall", i cannot ever be wrong.

[font="Times"] If anyone remembers me, I am back. Maybe to stay, maybe not.----------------------------------------------------------------------------------------------------------[/font][font="Times"]Things I am proud of: Pong! in AutoIt | SearchbarMy website: F.R.I.E.S.A little website that is trying to get started: http://thepiratelounge.net/ (not mine)[/font][font="Times"] ----------------------------------------------------------------------------------------------------------[/font][font="Arial"]The newbies need to stop stealing avatars!!! It is confusing!![/font]

Link to comment
Share on other sites

your avatar is weird.

[font="Times"] If anyone remembers me, I am back. Maybe to stay, maybe not.----------------------------------------------------------------------------------------------------------[/font][font="Times"]Things I am proud of: Pong! in AutoIt | SearchbarMy website: F.R.I.E.S.A little website that is trying to get started: http://thepiratelounge.net/ (not mine)[/font][font="Times"] ----------------------------------------------------------------------------------------------------------[/font][font="Arial"]The newbies need to stop stealing avatars!!! It is confusing!![/font]

Link to comment
Share on other sites

An alternative (though I didn't comment it):

Func _MonthName($format = 0)
    If $format = 0 Then
        Local $months = StringSplit("January,February,March,April,May,June,July,August,September,October,November,December", ",")
        Return $months[@MON]
    ElseIf $format = 1 Then
        $abbrev = StringSplit("Jan,Feb,Mar,Apr,June,July,Aug,Sept,Oct,Nov,Dec", ",")
        Return $abbrev[@MON] & "."
    Else
        SetError(1)
        Return ""
    EndIf
EndFunc

<{POST_SNAPBACK}>

Wouldn't this be shorter?

Func _MonthName($format = 0)
    Local $months = StringSplit("January,February,March,April,May,June,July,August,September,October,November,December", ",")
    If $format = 0 Then
        Return $months[@MON]
    ElseIf $format = 1 Then
        Return StringLeft($months[@MON], 3)
    Else
        SetError(1)
        Return ""
    EndIf
EndFunc

(Plus, as buckeyexp pointed out, CS forgot to put May in the $abbrev array)

Link to comment
Share on other sites

Wouldn't this be shorter?

Func _MonthName($format = 0)
    Local $months = StringSplit("January,February,March,April,May,June,July,August,September,October,November,December", ",")
    If $format = 0 Then
        Return $months[@MON]
    ElseIf $format = 1 Then
        Return StringLeft($months[@MON], 3)
    Else
        SetError(1)
        Return ""
    EndIf
EndFunc

(Plus, as buckeyexp pointed out, CS forgot to put May in the $abbrev array)

<{POST_SNAPBACK}>

Typically short dates spell out the full month name for June and July, so while technically shorter, it is also less conventional.
Link to comment
Share on other sites

Typically short dates spell out the full month name for June and July, so while technically shorter, it is also less conventional.

<{POST_SNAPBACK}>

You could have just posted the one-line fix, Valik :)

Well, it would be longer with a proper If...Else...EndIf block

Func _MonthName($format = 0)
    Local $months = StringSplit("January,February,March,April,May,June,July,August,September,October,November,December", ",")
    If $format = 0 Then
        Return $months[@MON]
    ElseIf $format = 1 Then
        If $months[@MON] <= 4 Then Return $months[@MON]
        Return StringLeft($months[@MON], 3);otherwise
    Else
        SetError(1)
        Return ""
    EndIf
EndFunc

Edit: As pointed out by Valik, this code also has a bug!

Edited by CyberSlug
Use Mozilla | Take a look at My Disorganized AutoIt stuff | Very very old: AutoBuilder 11 Jan 2005 prototype I need to update my sig!
Link to comment
Share on other sites

I think you're bugged again, CS. You're going to return January, February, March, and April. :)

You could have just posted the one-line fix, Valik  :D

How about I cut the number of lines in half?

Func _MonthName($nLongName = 0)
    Local $sAll = "January,February,March,April,May,June,July,August,September,October,November,December"
    Local $sMonth = StringMid($sAll, StringInStr($sAll, ",", 0, @MON - 1) + 1, StringInStr($sAll, ",", 0, @MON) - StringInStr($sAll, ",", 0, @MON - 1) - 1)
    If $nLongName Or StringLen($sMonth) <= 4 Then Return $sMonth
    Return StringLeft($sMonth, 3)
EndFunc

Edit: Fixed typo, added quote.

Edited by Valik
Link to comment
Share on other sites

:D Another good catch. I should give up posting to this thread... but not before I say the following:

Ha ha, function body is only TWO lines--and I tested this time! Watch for word wrap. :)

Func _MonthName($nLongName = 0)
    If $nLongName Or StringLen(StringMid("January,February,March,April,May,June,July,August,September,October,November,December", StringInStr("January,February,March,April,May,June,July,August,September,October,November,December", ",", 0, @MON - 1) + 1, StringInStr("January,February,March,April,May,June,July,August,September,October,November,December", ",", 0, @MON) - StringInStr("January,February,March,April,May,June,July,August,September,October,November,December", ",", 0, @MON - 1) - 1)) <= 4 Then Return StringMid("January,February,March,April,May,June,July,August,September,October,November,December", StringInStr("January,February,March,April,May,June,July,August,September,October,November,December", ",", 0, @MON - 1) + 1, StringInStr("January,February,March,April,May,June,July,August,September,October,November,December", ",", 0, @MON) - StringInStr("January,February,March,April,May,June,July,August,September,October,November,December", ",", 0, @MON - 1) - 1)
    Return StringLeft(StringMid("January,February,March,April,May,June,July,August,September,October,November,December", StringInStr("January,February,March,April,May,June,July,August,September,October,November,December", ",", 0, @MON - 1) + 1, StringInStr("January,February,March,April,May,June,July,August,September,October,November,December", ",", 0, @MON) - StringInStr("January,February,March,April,May,June,July,August,September,October,November,December", ",", 0, @MON - 1) - 1), 3)
EndFunc
Use Mozilla | Take a look at My Disorganized AutoIt stuff | Very very old: AutoBuilder 11 Jan 2005 prototype I need to update my sig!
Link to comment
Share on other sites

Typically short dates spell out the full month name for June and July, so while technically shorter, it is also less conventional.

<{POST_SNAPBACK}>

I see. I did not know that.

*Edit: Question. Valik, why did you use that method with StringMid and StringInStr instead of just using an array?

Based from your example, wouldn't this be more efficient?

Func _MonthName2($nLongName = 0)
    Local $aMonths = StringSplit("January,February,March,April,May,June,July,August,September,October,November,December", ",")
    If $nLongName Or StringLen($aMonths[@MON]) <= 4 Then Return $aMonths[@MON]
    Return StringLeft($aMonths[@MON], 3)
EndFunc

I'm not going to touch CS's code with a ten foot pole. :)

Edited by Saunders
Link to comment
Share on other sites

Saunders, I wasn't going for efficiency, I was simply demonstrating a different way of doing it and also cutting the length of the code down significantly. I'm also not a fan of the StringSplit trick. Run-time processing of something known at compile time seems a bit silly to me.

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...