icu Posted May 4, 2011 Share Posted May 4, 2011 (edited) 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 May 31, 2011 by icu Link to comment Share on other sites More sharing options...
Moderators Melba23 Posted May 4, 2011 Moderators Share Posted May 4, 2011 icu, Welcome to the AutoIt forum. 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 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 columnsChooseFileFolder ---- Single and multiple selections from specified path treeview listingDate_Time_Convert -- Easily convert date/time formats, including the language usedExtMsgBox --------- A highly customisable replacement for MsgBoxGUIExtender -------- Extend and retract multiple sections within a GUIGUIFrame ---------- Subdivide GUIs into many adjustable framesGUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView itemsGUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeViewMarquee ----------- Scrolling tickertape GUIsNoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxesNotify ------------- Small notifications on the edge of the displayScrollbars ----------Automatically sized scrollbars with a single commandStringSize ---------- Automatically size controls to fit textToast -------------- Small GUIs which pop out of the notification area Link to comment Share on other sites More sharing options...
hannes08 Posted May 4, 2011 Share Posted May 4, 2011 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. Regards,Hannes[spoiler]If you can't convince them, confuse them![/spoiler] Link to comment Share on other sites More sharing options...
smartee Posted May 4, 2011 Share Posted May 4, 2011 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. I'm sure you meant StringLeft($sShort_Month_Name_To, 3) Link to comment Share on other sites More sharing options...
AdmiralAlkex Posted May 4, 2011 Share Posted May 4, 2011 (edited) StringLeft() Edit: Cartman Edited May 4, 2011 by AdmiralAlkex .Some of my scripts: ShiftER, Codec-Control, Resolution switcher for HTC ShiftSome of my UDFs: SDL UDF, SetDefaultDllDirectories, Converting GDI+ Bitmap/Image to SDL Surface Link to comment Share on other sites More sharing options...
hannes08 Posted May 4, 2011 Share Posted May 4, 2011 I'm sure you meant StringLeft($sShort_Month_Name_To, 3) Uuupsi... Jep Sreingleft() Regards,Hannes[spoiler]If you can't convince them, confuse them![/spoiler] Link to comment Share on other sites More sharing options...
icu Posted May 4, 2011 Author Share Posted May 4, 2011 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 More sharing options...
smartee Posted May 4, 2011 Share Posted May 4, 2011 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 -smartee Link to comment Share on other sites More sharing options...
icu Posted May 4, 2011 Author Share Posted May 4, 2011 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 More sharing options...
smartee Posted May 5, 2011 Share Posted May 5, 2011 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 Link to comment Share on other sites More sharing options...
icu Posted May 5, 2011 Author Share Posted May 5, 2011 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 More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now