Jump to content

date function


Loftus
 Share

Recommended Posts

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?

Link to comment
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 2022-02-19 - Version 1.6.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (NEW 2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

Link to comment
Share on other sites

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

Link to comment
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?

Link to comment
Share on other sites

Specify a negative number of days to add. 

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2022-02-19 - Version 1.6.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (NEW 2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

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