mapl

Help on _DateAdd

9 posts in this topic

#1 ·  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

Share this post


Link to post
Share on other sites



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

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

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

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

Share this post


Link to post
Share on other sites

Thanks @boththose

 

Share this post


Link to post
Share on other sites

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.


[center]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.[/center]

Share this post


Link to post
Share on other sites

#7 ·  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

Share this post


Link to post
Share on other sites

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

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

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