DigDeep

Previous Month Name

4 posts in this topic

#1 ·  Posted (edited)

I can get the current month name as per below...

Local $sLongMonthName = _DateToMonth(@MON)

MsgBox(0, '', $sLongMonthName)

How can I get Previous Month name ?

Edited by DigDeep

Share this post


Link to post
Share on other sites



Shouldn't be that hard... something like:

#include<date.au3>
Local $PrevMonth = @MON-1
if @MON=1 then $PrevMonth = 12
Local $sPrevLongMonthName = _DateToMonth($PrevMonth)
ConsoleWrite('@@ Debug(' & @ScriptLineNumber & ') : $sPrevLongMonthName = ' & $sPrevLongMonthName & @CRLF & '>Error code: ' & @error & @CRLF)

Jos

1 person likes this

Visit the SciTE4AutoIt3 Download page for the latest versions        Beta files                                                          Forum Rules
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Share this post


Link to post
Share on other sites

also:

#include <date.au3>

ConsoleWrite("Last month was " & _CalcMonth() & @CRLF)
ConsoleWrite("5 Months ago was " & _CalcMonth(-5) & @CRLF)
ConsoleWrite("in 21 months will be " & _CalcMonth(21) & @CRLF)
ConsoleWrite("8 months before may is " & _CalcMonth(-8, 5) & @CRLF)


; This function Returns the name of the month of $n months ago or in $n months
; default is last month (from now)
; you can optionally pass as first parameter how many months you want to shift
; forward (by a positive integer) or backward (by a negative number)
; and optionally (by a second parameter) also from which month you want start the calculation
Func _CalcMonth($_iShift = -1, $_iMonth = @MON)
    Local $_sStartDate = @YEAR & "/" & StringFormat("%02s", $_iMonth) & "/01"
    $_iShift *= 1
    If _DateIsValid($_sStartDate) Then
        Local $aDatePart, $aTimePart
        _DateTimeSplit(_DateAdd("M", $_iShift, $_sStartDate), $aDatePart, $aTimePart)
        Return _DateToMonth($aDatePart[2], $DMW_LOCALE_LONGNAME)
    Else
        Return SetError(-1, 0, 0)
    EndIf
EndFunc   ;==>_CalcMonth

 


small minds discuss people average minds discuss events great minds discuss ideas.... and use AutoIt....

Share this post


Link to post
Share on other sites

#4 ·  Posted (edited)

Another simpler way to calculate the month number, that doesn't relies on Date.au3
Date.au3 is used for the nice multilingual _DateToMonth() function.

#include <date.au3>

ConsoleWrite("Last month was " & _DateToMonth(_CalcMonth(-1), $DMW_LOCALE_LONGNAME) & @CRLF)
ConsoleWrite("2 Months ago was " & _DateToMonth(_CalcMonth(-2), $DMW_LOCALE_LONGNAME) & @CRLF)
ConsoleWrite("in 21 months will be " & _DateToMonth(_CalcMonth(21), $DMW_LOCALE_LONGNAME) & @CRLF)
ConsoleWrite("3 months before may is " & _DateToMonth(_CalcMonth(-3, 5), $DMW_LOCALE_LONGNAME) & @CRLF)

; This function returns the number of the month of $n months ago or in $n months
; you pass as first parameter how many months you want to shift:
; forward (with a positive integer) or backward (with a negative integer)
; Optionally (with a second parameter) you can set the starting month for the calculation (default is current month)

Func _CalcMonth($_iShift = 0, $_iMonth = @MON)
    $_iShift = Int($_iShift)
    $_iMonth = Int($_iMonth)

    If $_iMonth < 1 Or $_iMonth > 12 Then Return SetError(-1, 0, 0) ; "Error: Wrong starting month"

    Local $x = Mod($_iShift + $_iMonth - 1, 12)
    Local $_iTarget = 1 + $x + (12 * ($x < 0))

    Return SetError(0, 0, $_iTarget)
EndFunc   ;==>_CalcMonth

 

Edited by Chimp
removed the multiply * 1 to parameters because not necessary (just int() is enough)

small minds discuss people average minds discuss events great minds discuss ideas.... and use AutoIt....

Share this post


Link to post
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