Jump to content

_DateToMonth not working? [solved]


Recommended Posts

Dear AutoIt Community,

Please help. I can't seem to get the _DateToMonth function to retrieve the abbreviated 'three-letter' name of the month.

I've gone over the help file, and checked <Date.au3> and can't see what I'm doing wrong.

The code is as follows:

#Include <Date.au3>
$Hyphen = "-"
$iDay = @MDAY
$iMonth = @MON
$iNext_Month = $iMonth + 1
$sShort_Month_Name_From = _DateToMonth ($iMonth, 1) ; Supposed to retrieve the abbreviated name
$sShort_Month_Name_To = _DateToMonth ($iNext_Month, 1) ; Supposed to retrieve the abbreviated name

MsgBox (0, "Date plus 1 Month", $sShort_Month_Name_From & " " & $sShort_Month_Name_To)

The result is a message box that keeps showing "June" when I want it to show "Jun".

Thanks for any help.

Edited by icu
Link to comment
Share on other sites

  • Moderators

icu,

Welcome to the AutoIt forum. :unsure:

showing "June" when I want it to show "Jun".

That is because Date.au3 defines the "shortname" as "June":

Local $aMonthNumber[13] = ["", "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"]
Local $aMonthNumberAbbrev[13] = ["", "Jan", "Feb", "Mar", "Apr", "May", "June", "July", "Aug", "Sept", "Oct", "Nov", "Dec"]

So to get "Jun" you will need to either write your own UDF or amend the current one! :>

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Link to comment
Share on other sites

Hi icu,

you could still go an do a StringRight($sShort_Month_Name_To, 3).

Also I have to say that it is weird that Jun and July have 4 characters. :unsure:

Regards,Hannes[spoiler]If you can't convince them, confuse them![/spoiler]
Link to comment
Share on other sites

Hi icu,

you could still go an do a StringRight($sShort_Month_Name_To, 3).

Also I have to say that it is weird that Jun and July have 4 characters. :unsure:

I'm sure you meant StringLeft($sShort_Month_Name_To, 3) :>

Link to comment
Share on other sites

Thanks to all who replied.

Melba23 your solution works, I basically edited the <Date.au3> file to read:

Local $aMonthNumberAbbrev[13] = ["", "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"

Hannes123 thanks for the pointer and smartee for the correct code example. If I didn't edit the <Date.au3> file I've worked out the code to be:

#Include <Date.au3>
$iMonth = @MON
$iNext_Month = $iMonth + 1
$sMonth_Long_From = _DateToMonth ($iMonth, 0)
$sMonth_Long_To = _DateToMonth ($iNext_Month, 0)
$sMonth_Short_From = StringLeft($sMonth_Long_From, 3)
$sMonth_Short_To = StringLeft($sMonth_Long_To, 3)

MsgBox (0, "Today's Month plus 1 Month", $sMonth_Short_From & " - " & $sMonth_Short_To)

NB: I know the above code breaks when @MON is for December (because the value of $iNext_Month will be "13" or "12 + 1")... I'm working a solution up using _DateAdd and StringSplit but I needed to learn how to get the three lettered month name into a string variable first.

Link to comment
Share on other sites

hi icu,

I know the above code breaks when @MON is for December (because the value of $iNext_Month will be "13" or "12 + 1")...

Simple workaround:
$iNext_Month = Mod($iMonth, 12) + 1

Hope this helps :unsure:

-smartee

Link to comment
Share on other sites

Hi smartee, your solution seems clever indeed!

I didn't even know Mod existed and it took me a while to think through its operation lol.

Thanks for the tip.

I ended up writing the follwing:

#Include <Date.au3> ; modified file with three letter months

; Create string for today's date in the format "DD-MMM-YYYY"

$iMonth = @MON
$iDay = @MDAY
$iYear = @YEAR
$sMonth_Short_Name = _DateToMonth ($iMonth, 1)
$vFrom_Date = $iDay & "-" & $sMonth_Short_Name & "-" & $iYear

; Create string for 1 month from today in the format "DD-MMM-YYYY"

$vNew_Date = _DateAdd( 'm', 1, _NowCalcDate())
$sNew_Date_Split = StringSplit ($vNew_Date, "/")
$iNew_Date_Day = $sNew_Date_Split[3]
$iNew_Date_Year = $sNew_Date_Split[1]
$sNew_Date_Month = _DateToMonth ($sNew_Date_Split[2], 1)
$vTo_Date = $iNew_Date_Day & "-" & $sNew_Date_Month & "-" & $iNew_Date_Year

MsgBox (0, "Today's date to next month's", $vFrom_Date & " - " & $vTo_Date)

It works perfect but seems like a bit of effort doesn't it? The problem I had was getting the "DD-MMM-YYYY" format right for a _IEPropertySet with a fussy website.

I'm amazed at how well written and documented AutoIt is--best Help file I've ever come across & 'Learning to Script with AutoIt V3' is the best free intro-to-coding book I've ever found. I first started to teach myself Perl about two years ago to do screen scraping and web automation stuff but I never really did much with it as it got very hard very quickly and I dropped my little coding projects because of it. But thanks to understanding the basics of Perl I've picked up AutoIt quite quickly... and considering how fast the community has helped with my question, and the scope of AutoIt, and the fact that it's free/open source, I'm frankly blown away.

My minor moan is that there isn't a 'Learning to Web Automate with AutoIt V3' book out there unless Google is lying to me or someone here has it on their desktop.

Thanks again everyone!

Link to comment
Share on other sites

Hi again icu,

Take a look at this, its a bit more concise and easier to incorporate especially if you need to work with multiple dates in this format (DD-MMM-YYYY),

#include <Date.au3>

Func _shortMonthFmt($date, $addType = "s", $addAmt = 0, $sep = "-")
    Local $dateArray = StringRegExp(_DateAdd($addType, $addAmt, $date), "\d+", 3)
    Return $dateArray[2] & $sep & StringUpper(StringLeft(_DateToMonth($dateArray[1]), 3)) & $sep & $dateArray[0]
EndFunc   ;==>_shortMonthFmt

;Example 1: Adding a month to today's date
$fromDate = _NowCalcDate() ;Today's date
MsgBox(0, "Example 1", _shortMonthFmt($fromDate) & " ==> " & _shortMonthFmt($fromDate, "m", 1))

;Example 2: Adding a day to yesterday's date
$fromDate = _DateAdd("d", -1, _NowCalcDate()) ;Yesterday's date
MsgBox(0, "Example 2", _shortMonthFmt($fromDate) & " ==> " & _shortMonthFmt($fromDate, "d", 1))

Regards

-smartee :unsure:

Link to comment
Share on other sites

Hi smartee,

Thanks so much for that bit of code, all the parts of it are really helpful. I'm not yet up with the play with making my own functions yet, or using regular expressions in AutoIt, and I didn't even know you could nestle functions within functions (makes sense now upon reflection)... so it's really helpful of you to point the way.

Cheers!

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