Jump to content

Help on _DateAdd


Go to solution Solved by kylomas,

Recommended Posts

Posted (edited)

#include <DateTimeConstants.au3>
#include <GUIConstantsEx.au3>
#include <MsgBoxConstants.au3>
#Include <Date.au3>

HotKeySet("{ESC}", "Terminate")


GUICreate("Look-Up Tool", 350, 120, -1, -1)

Global $idDate = GUICtrlCreateDate("", 10, 10, 180, 20, $DTS_SHORTDATEFORMAT) ; Date Picker
Global $idTime = GUICtrlCreateDate("", 10, 40, 180, 20, $DTS_TIMEFORMAT) ; Time Picker

$DTM_SETFORMAT_ = 0x1032
$style = "HH:mm"
GUICtrlSendMsg($idTime, $DTM_SETFORMAT_, 0, $style)

Global $idCalculate = GUICtrlCreateButton("Calculate Date/Time", 10, 90, 330, 20)

GUICtrlSetOnEvent($idCalculate, "Calculate")
GUICtrlCreateLabel("Result", 245, 10)
GUISetState(@SW_SHOW)

Local $iMsg = 0
While 1
    $iMsg = GUIGetMsg()
    Switch $iMsg
         Case $idCalculate
            Calculate()
         Case $GUI_EVENT_CLOSE
            ExitLoop
    EndSwitch
WEnd

Func Terminate()
    Exit 0 ; Terminate via ESC
EndFunc

Func Calculate()
   GUICtrlCreateLabel(GUICtrlRead($idDate), 235, 30) ; Display Date
   GUICtrlCreateLabel(GUICtrlRead($idTime), 235, 50) ; Display Time
   GUICtrlCreateLabel(_DateAdd('h', -4, GUICtrlRead($idTime)), 235, 70) ; Subtract 4 hours.
EndFunc

Program will basically subtract 4 hours from chosen date and time.

Not sure why the line

"GUICtrlCreateLabel(_DateAdd('h', -4, GUICtrlRead($idTime)), 235, 50) ; Subtract 4 hours."

gives out "0".

Any help will be appreciated.

Edited by mapl
Posted

Because (according to the help file) _DateAdd needs the date and the time in format "YYYY/MM/DD[ HH:MM:SS]".

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2024-07-28 - Version 1.6.3.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 (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

 

Posted

BTW:

Could you please enclose your code in AutoIt code tags (in the editor click on the blue "A" icon). That greatly enhances readability.

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2024-07-28 - Version 1.6.3.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 (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

 

Posted

this would fix it, until a smart regexp shows up

Func Calculate()
   GUICtrlCreateLabel(GUICtrlRead($idDate), 235, 30) ; Display Date
   GUICtrlCreateLabel(GUICtrlRead($idTime), 235, 50) ; Display Time
   $sMinus4hr = _DateAdd('h', -4,  "2015/01/01 " & GUICtrlRead($idTime))
   $aMinus4hr = stringsplit($sMinus4hr , " " , 3)
   GUICtrlCreateLabel($aMinus4hr[1], 235, 70) ; Subtract 4 hours.
EndFunc

,-. .--. ________ .-. .-. ,---. ,-. .-. .-. .-.
|(| / /\ \ |\ /| |__ __||| | | || .-' | |/ / \ \_/ )/
(_) / /__\ \ |(\ / | )| | | `-' | | `-. | | / __ \ (_)
| | | __ | (_)\/ | (_) | | .-. | | .-' | | \ |__| ) (
| | | | |)| | \ / | | | | | |)| | `--. | |) \ | |
`-' |_| (_) | |\/| | `-' /( (_)/( __.' |((_)-' /(_|
'-' '-' (__) (__) (_) (__)

  • Moderators
Posted

Are you creating labels over and over intentionally, or would you prefer to only create 1 time and fill the data in?  The method you're currently using is like having a memory leak.

Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

  • Solution
Posted (edited)

mapl,

Try this...

#include <DateTimeConstants.au3>
#include <GUIConstantsEx.au3>
#include <MsgBoxConstants.au3>
#Include <Date.au3>

HotKeySet("{ESC}", "Terminate")


GUICreate("Look-Up Tool", 350, 120, -1, -1)

Global $idDate = GUICtrlCreateDate("", 10, 10, 180, 20, $DTS_SHORTDATEFORMAT) ; Date Picker
Global $idTime = GUICtrlCreateDate("", 10, 40, 180, 20, $DTS_TIMEFORMAT) ; Time Picker

$DTM_SETFORMAT_ = 0x1032
$style = "HH:mm"
GUICtrlSendMsg($idTime, $DTM_SETFORMAT_, 0, $style)

Global $idCalculate = GUICtrlCreateButton("Calculate Date/Time", 10, 90, 330, 20)

GUICtrlSetOnEvent($idCalculate, "Calculate")
GUICtrlCreateLabel("Result", 245, 10)
local $strt010 = guictrlcreatelabel('',245,30,100,20)
local $diff010 = guictrlcreatelabel('',245,50,100,20)
GUISetState(@SW_SHOW)

Local $iMsg = 0
While 1
    $iMsg = GUIGetMsg()
    Switch $iMsg
         Case $idCalculate
            Calculate()
         Case $GUI_EVENT_CLOSE
            ExitLoop
    EndSwitch
WEnd

Func Terminate()
    Exit 0 ; Terminate via ESC
EndFunc

Func Calculate()
    guictrlsetdata($strt010, guictrlread($idDate) & ' ' & guictrlread($idTime))
    guictrlsetdata($diff010,_dateadd('h', -4, stringregexpreplace(guictrlread($idDate),'(\d+)/(\d+)/(\d\d\d\d)','$3/$1/$2') & ' ' & GUICtrlRead($idTime)))
EndFunc

  DateAdd needs the date/time parm in a specific format.  Also, as SmOke_N said, you are recreating controls needlessly.

kylomas

edit: Also the HotKey is not necessary to exit the script.

Edited by kylomas

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

Posted

also, this stringsplit syntax i am just getting used to

Func Calculate()
   GUICtrlCreateLabel(GUICtrlRead($idDate), 235, 30) ; Display Date
   GUICtrlCreateLabel(GUICtrlRead($idTime), 235, 50) ; Display Time
   GUICtrlCreateLabel(stringsplit(_DateAdd('h', -4 , "2015/01/01" & " " & GUICtrlRead($idTime)), " " , 3)[1] , 235, 70)
EndFunc

,-. .--. ________ .-. .-. ,---. ,-. .-. .-. .-.
|(| / /\ \ |\ /| |__ __||| | | || .-' | |/ / \ \_/ )/
(_) / /__\ \ |(\ / | )| | | `-' | | `-. | | / __ \ (_)
| | | __ | (_)\/ | (_) | | .-. | | .-' | | \ |__| ) (
| | | | |)| | \ / | | | | | |)| | `--. | |) \ | |
`-' |_| (_) | |\/| | `-' /( (_)/( __.' |((_)-' /(_|
'-' '-' (__) (__) (_) (__)

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
×
×
  • Create New...