Loftus

date function

6 posts in this topic

I need to automate loading forms and want to use a series of past dates.   _NowDate, _DateAdd and _NowCalcDate functions will do what I want with one exception; the date format.  I need to return the format of mm/dd/yyyy, not yyyy/mm/dd.  How can I do that?

Share this post


Link to post
Share on other sites



Welcome to AutoIt and the forum!
Please check Melba's date/time conversion UDF:

 


My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2017-04-18 - Version 1.4.8.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2017-02-27 - Version 1.3.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2015-04-01 - Version 0.4.0.0) - Download - General Help & Support - Example Scripts
Excel - Example Scripts - Wiki
Word - Wiki
PowerPoint (2015-06-06 - Version 0.0.5.0) - Download - General Help & Support

Tutorials:
ADO - Wiki

 

Share this post


Link to post
Share on other sites

#3 ·  Posted (edited)

Loftus,

...or roll your own...

#include <GUIConstantsEx.au3>
#include <GUIConstants.au3>
#include <date.au3>

Local $gui010 = GUICreate('Date Convert')

Local $datein = GUICtrlCreateDate('', 10, 10, 200, 20)
Local $sStyle = "yyyy/MM/dd"
GUICtrlSendMsg($datein, $DTM_SETFORMATW, 0, $sStyle)

GUICtrlCreateLabel('Converted date', 10, 50, 200, 20)
Local $dateconvert = GUICtrlCreateLabel('', 10, 75, 200, 20, $ss_sunken)

GUISetState()

While 1
    $msg = GUIGetMsg()
    Switch $msg
        Case $gui_event_close
            Exit
        Case $datein
            GUICtrlSetData($dateconvert, _convertdate(GUICtrlRead($datein)))
    EndSwitch
WEnd

Func _convertdate($str)

    Return StringRegExpReplace($str, '(\d\d\d\d)/(\d\d)/(\d\d)', '$2/$3/$1') ; Swap yyyy/mm/dd to mm/dd/yyyy

EndFunc   ;==>_convertdate

kylomas

edit: if you need to zero fill mm or dd then you can do this

Return StringRegExpReplace(StringRegExpReplace($dtmDate, '(\d{4})(\d{2})(\d{2})(\d{2})(\d{2})(\d{2}).*', '$2/$3/$1 $4:$5:$6'), '(?<!\d)(\d/)', '0$1')

 

Edited by kylomas
code cleanup

Forum Rules         Procedure for posting code

"I like pigs.  Dogs look up to us.  Cats look down on us.  Pigs treat us as equals."

- Sir Winston Churchill

Share this post


Link to post
Share on other sites

...as does Test2() in the example on the StringRegExpReplace() page in the help file  :)

Share this post


Link to post
Share on other sites

Thank you.  I have the date formatting correctly but I cannot get the previous date to calculate like I can in other languages.  Similar to     DateAdd('d', -1, Date()).  this calculates yesterday or n days in the past.  Everything I can find about AutoIt _DateAdd refers to adding a date, not subtracting a date.  Is it possible to subtract a date from a given date?

Share this post


Link to post
Share on other sites

Specify a negative number of days to add. 


My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2017-04-18 - Version 1.4.8.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2017-02-27 - Version 1.3.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2015-04-01 - Version 0.4.0.0) - Download - General Help & Support - Example Scripts
Excel - Example Scripts - Wiki
Word - Wiki
PowerPoint (2015-06-06 - Version 0.0.5.0) - Download - General Help & Support

Tutorials:
ADO - Wiki

 

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