Jump to content

Date calculator: Add to or subtract from a date (Not Today)


SDG
 Share

Go to solution Solved by SDG,

Recommended Posts

Hello all,

I am new with Autoit, I'm curious if there is a way to read the output of the calendar for a date in the past or the future and add days, weeks, month or/and years to the chosen calender date?

similar to this webpage 

http://www.timeanddate.com/date/dateadded.html?d1=15&m1=4&y1=2014&type=add&ay=&am=&aw=3&ad=

for now I just need to add  5 days to a chosen Date

I have this small script 

$Date1 = GUICtrlCreateDate("2014/01/29 20:46:39", 60, 10, 200, 25)
$Calculate = GUICtrlCreateButton("Calculate", 120, 60, 75, 25)
$Label = GUICtrlCreateLabel("Three weeks from entered date", 16, 100, 150, 20)
$Input1 = GUICtrlCreateInput("", 180, 100, 130, 21)
GUICtrlSetState(-1, $GUI_DISABLE)
GUISetState(@SW_SHOW)
While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit

               Case $Calculate
                  $InputDate = Int(GUICtrlRead($Date1))
                  $sNewDate =  _DateAdd( 'd',5, _NowCalcDate())
                  $myCalc = Stringreplace ( $sNewDate , "/", "-")
                  GUICtrlSetData($Input1, string($myCalc))
    EndSwitch
WEnd

Many thanks in advance

Edited by SDG
Link to comment
Share on other sites

You're reading from the Date control and putting it into $InputDate, but you're using _NowCalcDate in your _DateAdd, probably not what you wanted to do is it?

If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Link to comment
Share on other sites

You're reading from the Date control and putting it into $InputDate, but you're using _NowCalcDate in your _DateAdd, probably not what you wanted to do is it?

 

Hi BrewManNH,

No, that is just to make the script running, what I've done will add 5 days to Today's Date. 

And what I'm looking for, is to choose a date from the calender like " 5th Feb 2014"and add 5 days to it 

Feel free to rewrite the code as you wish to come close to the desired result

Edited by SDG
Link to comment
Share on other sites

so far I've it with help of Google like this

#include <GuiDateTimePicker.au3>
#include <Date.au3>
#include <ButtonConstants.au3>
$hGui = GUICreate("Date calculator: Add to or subtract from a date", 336, 130, 100, 100)
    $hdate = GUICtrlCreateDate(_Now(), 45,20, 250, 20, 0x00)
    $hWndDate = ControlGetHandle($hGui, "", $hdate)
    _GUICtrlDTP_SetFormat($hWndDate, "yyyy.MM.dd dddd")
    GUISetState()
    $Calculate = GUICtrlCreateButton("Calculate", 120, 60, 75, 25)
$Label = GUICtrlCreateLabel("Three weeks from entered date", 16, 100, 150, 20)
$Input1 = GUICtrlCreateInput("", 180, 100, 130, 21)
GUISetState(@SW_SHOW)
While 1
    $msg = GUIGetMsg()
    Switch $msg
        Case -3
            Exit
               Case $Calculate
                  $myCalc = GUICtrlRead($hdate)
                  GUICtrlSetData($Input1, string($myCalc))
                  ; I want to add 5 days to the output meaning $mycalc_value plus 5 days
    EndSwitch
WEnd

now, how can I add fixed days to the outcome? If I choose the 5th Jan 2014 on the calender I want the output to be the 10th Jan 2014.

Many thanks in advance

Link to comment
Share on other sites

#include <GuiDateTimePicker.au3>
#include <Date.au3>
#include <ButtonConstants.au3>
$hGui = GUICreate("Date calculator: Add to or subtract from a date", 336, 130, 100, 100)
    $hdate = GUICtrlCreateDate(_Now(), 45,20, 250, 20, 0x00)
    $hWndDate = ControlGetHandle($hGui, "", $hdate)
    _GUICtrlDTP_SetFormat($hWndDate, "yyyy/MM/dd")
    GUISetState()
    $Calculate = GUICtrlCreateButton("Calculate", 120, 60, 75, 25)
$Label = GUICtrlCreateLabel("Three weeks from entered date", 16, 100, 150, 20)
$Input1 = GUICtrlCreateInput("", 180, 100, 130, 21)
GUISetState(@SW_SHOW)
While 1
    $msg = GUIGetMsg()
    Switch $msg
        Case -3
            Exit
               Case $Calculate
                  $myCalc = GUICtrlRead($hdate)                  
                  GUICtrlSetData($Input1, _DateAdd("D", 5, $myCalc))

                  ; I want to add 5 days to the output meaning $mycalc_value plus 5 days
    EndSwitch
WEnd

Link to comment
Share on other sites

  • Solution
#include <GuiDateTimePicker.au3>
#include <Date.au3>
#include <ButtonConstants.au3>
$hGui = GUICreate("Date calculator: Add to or subtract from a date", 336, 130, 100, 100)
    $hdate = GUICtrlCreateDate(_Now(), 45,20, 250, 20, 0x00)
    $hWndDate = ControlGetHandle($hGui, "", $hdate)
    _GUICtrlDTP_SetFormat($hWndDate, "yyyy/MM/dd")
    GUISetState()
    $Calculate = GUICtrlCreateButton("Calculate", 120, 60, 75, 25)
$Label = GUICtrlCreateLabel("Three weeks from entered date", 16, 100, 150, 20)
$Input1 = GUICtrlCreateInput("", 180, 100, 130, 21)
GUISetState(@SW_SHOW)
While 1
    $msg = GUIGetMsg()
    Switch $msg
        Case -3
            Exit
               Case $Calculate
                  $myCalc = GUICtrlRead($hdate)                  
                  GUICtrlSetData($Input1, _DateAdd("D", 5, $myCalc))

                  ; I want to add 5 days to the output meaning $mycalc_value plus 5 days
    EndSwitch
WEnd

Many thanks Geir1983, your code is working fine.

It worked

I worked on something similar 

#include <GuiDateTimePicker.au3>
#include <Date.au3>
#include <ButtonConstants.au3>
$hGui = GUICreate("Date calculator: Add to or subtract from a date", 336, 130, 100, 100)
    $hdate = GUICtrlCreateDate(_Now(), 45,20, 250, 20, 0x00)
    $hWndDate = ControlGetHandle($hGui, "", $hdate)
    _GUICtrlDTP_SetFormat($hWndDate, "yyyy-MM-dd")
    GUISetState()
    $Calculate = GUICtrlCreateButton("Calculate", 120, 60, 75, 25)
$Label = GUICtrlCreateLabel("Three weeks from entered date", 16, 100, 150, 20)
$Input1 = GUICtrlCreateInput("", 180, 100, 130, 21)
GUISetState(@SW_SHOW)
While 1
    $msg = GUIGetMsg()
    Switch $msg
        Case -3
            Exit
         Case $Calculate
            $sNewDate =  _DateAdd("w", 3, GUICtrlRead($hdate))
            $myCalc = Stringreplace ( $sNewDate , "/", "-")
                  GUICtrlSetData($Input1, string($myCalc))
    EndSwitch
WEnd
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...