Jump to content

Autoit Code for this VB Code


Recommended Posts

Hi Guys,

Just wondering how i would re-create the same code for Autoit using this VBA code:

Datex = StrConv(Format(Now(), "ddmmMyy"), vbUpperCase)

Which basically simply means for example today is 23/08/13 (Australia) i use it to show 23AUG13 which i use to save files using todays date in that format.

Anyway to re-create this in autoit as i need to be able to open 'filepath' & todays date? i know to get todays date its:

#include <Date.au3>
_NowDate()

Cant find the format in the help file, well i can but it doesnt mention about how i go about changing it to read 23AUG13

Edited by 13lack13lade
Link to comment
Share on other sites

>_Date_Time_Convert is a User Defined Function which appears to work.


#include <Date.au3>

ConsoleWrite(_Date_Time_Convert(_NowDate(), "dd/MM/yyyy", "ddMMMyy") & @LF); Returns 23AUG13


; http://www.autoitscript.com/forum/topic/112520-date-time-convert/#entry788257
; #FUNCTION# ==================================================================================
; Name...........: _Date_Time_Convert
; Description ...: Converts a date and/or time from its existing format to any other custom format.
; Example Date time "Thursday, 1 April, 2010 @ 08:05:08 PM"
; Example's Format Eg : "dddd, d MMMM, yyyy @ hh:mm:ss tt"
; Year. : yyyy = 2010 ; yy = 10
; Month : MMMM = April; MMM = Apr; MM = 04; M = 4
; Day.. : dddd = Thursday; ddd = Thu; dd = 01; d = 1
; Hour. : HH = 20; H = 20 (1 digit minimum); hh = 08; h = 8 (Lowercase h's used with AM/PM time)
; Minute: mm = 05 (2 digit minimum); m = 5 (1 digit minimum)
; Second: ss = 08 (2 digit minimum); s = 8 (1 digit minimum)
; AM/PM : tt = AM or PM; t = A or P
; Ref: Format Strings @ <a href='http://msdn.microsoft.com/en-us/library/bb761726(v=VS.85).aspx#dtp_format_chars' class='bbc_url' title='External link' rel='nofollow external'>http://msdn.microsoft.com/en-us/library/bb761726(v=VS.85).aspx#dtp_format_chars</a>
; =============================================================================================

Func _Date_Time_Convert($sDateTime, $sDateTimeFormat, $sRetFormat = "yyyy/MM/dd HH:mm:ss")
    Local $Time, $iYear, $iMnth, $iDay, $iHour, $iMinute, $iSec, $aDTFormat, $aDTVal, $iYearLen
    Local $aMMM[13] = [12, "jan", "feb", "mar", "apr", "may", "jun", "jul", "aug", "sep", "oct", "nov", "dec"]

    $aDTFormat = StringRegExp($sDateTimeFormat, "yyyy|yy|MMMM|MMM|MM|M|dddd|ddd|dd|d|HH|H|hh|h|mm|m|ss|s|tt|t|\d+|[[:punct:]]", 3)
    $aDTVal = StringRegExp($sDateTime, "[a-zA-Z]+|\d{1,4}|[[:punct:]]", 3)
    If UBound($aDTFormat) <> UBound($aDTVal) Then
        MsgBox(0, "ERROR", " Possibly need a delineator between the digit values for year, month, day, hour, minute, or second.", 5)
        Return
    EndIf
    For $i = 0 To UBound($aDTFormat) - 1
        Select
            Case $aDTFormat[$i] == "yy" Or $aDTFormat[$i] == "yyyy" ; y - Year
                $iYearLen = StringLen($aDTFormat[$i])
                If $iYearLen = 4 Then $iYear = $aDTVal[$i] & "/"
                If $iYearLen = 2 Then
                    If Number($aDTVal[$i]) < Number(StringRight(@YEAR, 2) + 20) Then
                        $iYear = "20" & $aDTVal[$i] & "/"
                    Else
                        $iYear = "19" & $aDTVal[$i] & "/"
                    EndIf
                EndIf

            Case $aDTFormat[$i] == "M" Or $aDTFormat[$i] == "MM" Or $aDTFormat[$i] == "MMM" Or $aDTFormat[$i] == "MMMM" ; M - Month
                If StringLen($aDTFormat[$i]) > 2 Then
                    For $j = 1 To UBound($aMMM) - 1
                        If StringLeft($aDTVal[$i], 3) = $aMMM[$j] Then $aDTVal[$i] = $j
                    Next
                EndIf
                $iMnth = StringRight("0" & $aDTVal[$i], 2) & "/"

            Case $aDTFormat[$i] == "d" Or $aDTFormat[$i] == "dd" ; d - Day
                $iDay = StringRight("0" & $aDTVal[$i], 2) & " "

            Case $aDTFormat[$i] == "h" Or $aDTFormat[$i] == "hh" ; or StringRegExp( $aDTFormat[$i],"(?i)hh?tt?");  h - Hour
                $iHour = $aDTVal[$i]
                If $iHour = 12 Then $iHour = 0
                For $k = 0 To UBound($aDTFormat) - 1
                    If ($aDTFormat[$k] == "t" Or $aDTFormat[$k] == "tt") And StringLeft($aDTVal[$k], 1) = "p" Then $iHour = Mod(12 + $iHour, 24)
                Next
                $iHour = StringRight("0" & $iHour, 2) & ":"

            Case $aDTFormat[$i] == "H" Or $aDTFormat[$i] == "HH" ;  H - Hour
                $iHour = StringRight("0" & $aDTVal[$i], 2) & ":"

            Case $aDTFormat[$i] == "m" Or $aDTFormat[$i] == "mm" ;or StringRegExp( $aDTFormat[$i],"(?i)mm?tt?") ; m - Minute
                $iMinute = StringRight("0" & $aDTVal[$i], 2) & ":"

            Case $aDTFormat[$i] == "s" Or $aDTFormat[$i] == "ss" ; or StringRegExp( $aDTFormat[$i],"(?i)ss?tt?") ; s - Second
                $iSec = StringRight("0" & $aDTVal[$i], 2) & ":"
        EndSelect
    Next

    ; Default values added to empty, unused variables for entry into the Date Control.
    If $iYear = "" Then $iYear = "1900/"
    If $iMnth = "" Then $iMnth = "01/"
    If $iDay = "" Then $iDay = "01 "
    If $iHour = "" Then $iHour = "00:"
    If $iMinute = "" Then $iMinute = "00:"
    If $iSec = "" Then $iSec = "00"
    $Time = $iYear & $iMnth & $iDay & $iHour & $iMinute & $iSec

    ;===== The following converts $Time to $sRetFormat format using Date Control ======
    ; $Time is now in this format "yyyy/MM/dd HH:mm:ss"
    Local $hGui = GUICreate("My GUI get date", 200, 200, 800, 200)
    Local $idDate = GUICtrlCreateDate($Time, 10, 10, 185, 20)
    GUICtrlSendMsg($idDate, 0x1032, 0, $sRetFormat)
    Local $sReturn = GUICtrlRead($idDate)
    GUIDelete($hGui)
    Return $sReturn
EndFunc   ;==>_Date_Time_Convert

Edit: And here is a dedicated date convert function from "dd/MM/yyyy" format to "ddMMMyy" format.

#include <Date.au3>

ConsoleWrite(_Date_Convert(_NowDate()) & @LF); Returns 23AUG13


Func _Date_Convert($sDate)
    Local $aMMM[13] = [12, "JAN", "FEB", "MAR", "APR", "MAY", "JUN", "JUL", "AUG", "SEP", "OCT", "NOV", "DEC"]
    Return Execute(StringRegExpReplace($sDate, "(\d{2})/(\d{2})/(\d{2})(\d{2})", '"\1" & ' & '$aMMM[\2]' & ' & "\4"'))
EndFunc   ;==>_Date_Convert
Edited by Malkey
Link to comment
Share on other sites

This example joins strings together with some of the strings stored in variables.
Then uses the variable that has the full file name to open that file if it exists.

#include <Date.au3>

Local $sPath = "C:\TEMP\"
Local $sPartFileName = _Date_Convert(_NowDate()); Returns 23AUG13
Local $sFullFileName = $sPath & "File_" & $sPartFileName & ".txt" ; String concatenation (join strings together)
ConsoleWrite( $sFullFileName & @LF) ; Displays C:\TEMP\File_26AUG13.txt

If FileExists($sFullFileName) Then

    ; ---- Modified example from AutoIt help file ------
    Local $file = FileOpen($sFullFileName, 0) ; <------- Opens the file (read only) whose name is stored in the $sFullFileName variable.

    ; Check if file opened for reading OK
    If $file = -1 Then
        MsgBox(0, "Error", "Unable to open file.")
        Exit
    EndIf

    FileClose($file) ; <- Note the file handle is used to identify the opened file, NOT the file name.
    ; ----> End of modified example from AutoIt help file ------

EndIf


Func _Date_Convert($sDate)
    Local $aMMM[13] = [12, "JAN", "FEB", "MAR", "APR", "MAY", "JUN", "JUL", "AUG", "SEP", "OCT", "NOV", "DEC"]
    Return Execute(StringRegExpReplace($sDate, "(\d{2})/(\d{2})/(\d{2})(\d{2})", '"\1" & ' & '$aMMM[\2]' & ' & "\4"'))
EndFunc   ;==>_Date_Convert
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...