Jump to content

Get month from date


Recommended Posts

Hi Guys,

I have the following code:

#include <Date.au3>
;~ Substract one month from the current month
$newmonth = _DateAdd('M', -1, _NowCalcDate())
;~ Write the new date
ConsoleWrite(@LF&@LF&$newmonth&@LF&@LF)

The result is: 2017/12/16

What I need is to use only the month from this date, so remove the Year and the Day and leave only the month.

Issue is that if I use:

;~ Get current month
$currentmonth = @MON
;~ Substract one month
$newmonth = $currentmonth-1
;~ Write the details
ConsoleWrite(@LF&@LF&$newmonth&@LF&@LF)

The result is Zero (0) so I will need to use the Date.au3 UDF (correct me if I am wrong).

Would you be able to assist me?

Link to comment
Share on other sites

Once way is using StringSplit which create an array and then you can select the month.

 

#include <Date.au3>
;~ Substract one month from the current month
$newmonth = _DateAdd('M', -1, _NowCalcDate())
;~ Write the new date

If $newmonth <> 0 Then
    $Temp = StringSplit($newmonth, '/')
    $newmonth = $Temp[2]
EndIf
ConsoleWrite(@LF&@LF&$newmonth&@LF&@LF)

 

AutoIt Scripts:NetPrinter - Network Printer UtilityRobocopyGUI - GUI interface for M$ robocopy command line
Link to comment
Share on other sites

8 minutes ago, Danp2 said:

A couple of thoughts --

1) You could use _DateTimeSplit to get the separated month component after the initial _DateAdd.

2) You could try something like this --

$newmonth = (@MON = 1) ? 12 : @MON - 1

 

Thank you for the suggestion. I did not know that _DateTimeSplit can do this, and this works perfectly for me.

3 minutes ago, Danny35d said:

Once way is using StringSplit which create an array and then you can select the month.

 

#include <Date.au3>
;~ Substract one month from the current month
$newmonth = _DateAdd('M', -1, _NowCalcDate())
;~ Write the new date

If $newmonth <> 0 Then
    $Temp = StringSplit($newmonth, '/')
    $newmonth = $Temp[2]
EndIf
ConsoleWrite(@LF&@LF&$newmonth&@LF&@LF)

 

Thank you for the suggestion, I used Danp2's one.

The new code:

#include <Date.au3>

Local $aMyDate, $aMyTime

$newmonth = _DateAdd('M', -1, _NowCalcDate())
_DateTimeSplit($newmonth, $aMyDate, $aMyTime)

ConsoleWrite(@LF&@LF&$aMyDate[2]&@LF&@LF)

Thanks everyone!

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