Jump to content

Best way to use _DateAdd with invalid Format?


litlmike
 Share

Recommended Posts

I am wanting to know if this is the best way to accomplish this.

Scenario:

-Input #1 is a Date in the format DD/MM/YYYY

-Input #2 is the number of days to add to the above Date

-Output should be the adjusted date in format DD/MM/YYYY

The only way I can think of accomplishing this in AutoIt is to:

-Get date in DD/MM/YYYY

-String Split DD, MM, and YYYY

-Save DD, MM, YYYY as variables

-Make string as YYYY/MM/DD

-Use _DateAdd() to add to date

-String Split new date YYYY, MM, and DD

-Make new string as DD/MM/YYYY (which gives the final output)

I can't seem to find any UDF to simply this procedure, is this really the easiest way to do this?

TIA

EDIT: Typo

Edited by litlmike
Link to comment
Share on other sites

I am wanting to know if this is the best way to accomplish this.

Scenario:

-Input #1 is a Date in the format DD/MM/YYYY

-Input #1 is the number of days to add to the above Date

-Output should be the adjusted date in format DD/MM/YYYY

The only way I can think of accomplishing this in AutoIt is to:

-Get date in DD/MM/YYYY

-String Split DD, MM, and YYYY

-Save DD, MM, YYYY as variables

-Make string as YYYY/MM/DD

-Use _DateAdd() to add to date

-String Split new date YYYY, MM, and DD

-Make new string as DD/MM/YYYY (which gives the final output)

I can't seem to find any UDF to simply this procedure, is this really the easiest way to do this?

TIA

Use _nowcalc() and _datediff()

My Projects: [topic="89413"]GoogleHack Search[/topic], [topic="67095"]Swiss File Knife GUI[/topic], [topic="69072"]Mouse Location Pointer[/topic], [topic="86040"]Standard Deviation Calculator[/topic]

Link to comment
Share on other sites

Use _nowcalc() and _datediff()

What has been posted indicates to me that Input #1 allows the user to input a date which means that _nowcalc() will not be the answer. The solution posted by the OP will work but I would also take a look at using 3 inputs to replace Input #1. One for the year, one for the month and the last for the day. He can then feed that data into _DateAdd in the proper format.

Edit: You may also want to take a look at GUICtrlCreateDate() to replace the input #1.

Edited by GEOSoft

George

Question about decompiling code? Read the decompiling FAQ and don't bother posting the question in the forums.

Be sure to read and follow the forum rules. -AKA the AutoIt Reading and Comprehension Skills test.***

The PCRE (Regular Expression) ToolKit for AutoIT - (Updated Oct 20, 2011 ver:3.0.1.13) - Please update your current version before filing any bug reports. The installer now includes both 32 and 64 bit versions. No change in version number.

Visit my Blog .. currently not active but it will soon be resplendent with news and views. Also please remove any links you may have to my website. it is soon to be closed and replaced with something else.

"Old age and treachery will always overcome youth and skill!"

Link to comment
Share on other sites

Thanks to all for the attention to this, however, neither solution will work. Maybe I should have expressed the limitations with this. The date input cannot be expressed in any other format, other than the way it currently is expressed. Meaning, the date is taken from a Windows Control and must be expressed as DD/MM/YYYY. It must be inputted and outputted as DD/MM/YYYY. Any other solutions available to this?

Thanks.

Link to comment
Share on other sites

What has been posted indicates to me that Input #1 allows the user to input a date which means that _nowcalc() will not be the answer. The solution posted by the OP will work but I would also take a look at using 3 inputs to replace Input #1. One for the year, one for the month and the last for the day. He can then feed that data into _DateAdd in the proper format.

Edit: You may also want to take a look at GUICtrlCreateDate() to replace the input #1.

As I mentioned above use what I stated:

#include <GUIConstants.au3>
#include <Date.au3>

GUICreate ( "Date", 200,200,800,200)
$date = GUICtrlCreateDate (_DateTimeFormat( _NowCalc(),2), 10,10,185,20 )
$input1 = GUICtrlCreateInput(@MON,10,40,40,20)
$input2 = GUICtrlCreateInput(@MDAY,60,40,40,20)
$input3 = GUICtrlCreateInput(@YEAR,120,40,60,20)

$style = "yyyy/MM/dd"
_formatdate($date,$style)

$calc = GUICtrlCreateButton("Calc",10,70,100,20)
$msg = GUIGetMsg()

GuiSetState()
While 1 
    $msg = GUIGetMsg()
    Select
        Case $msg = $GUI_EVENT_CLOSE
            Exit
        Case $msg = $calc
            _calc()
    EndSelect
WEnd

Func _calc()
    $olddate = GUICtrlRead($date)
    $mm = GUICtrlRead($input1)
    $dd = GUICtrlRead($input2)
    $yy = GUICtrlRead($input3)
    $formatdate = $yy & "/" & $mm & "/" & $dd
    $diffdate = _DateDiff('d',$olddate,$formatdate)
    MsgBox(0,"Difference =",$diffdate & " days")
    $newdate = _DateAdd( 'd',$diffdate, $olddate)
    $date2 = GUICtrlCreateDate ($newdate, 10,100,185,20 )
    $style = "MM/dd/yyyy"
    _formatdate($date2,$style)
EndFunc

Func _formatdate($dte,$style)
    ; to select a specific default format
    If @UNICODE Then
        $DTM_SETFORMAT_ = 0x1032
    Else
        $DTM_SETFORMAT_ = 0x1005
    Endif
    GuiCtrlSendMsg($dte, $DTM_SETFORMAT_, 0, $style)
EndFunc

My Projects: [topic="89413"]GoogleHack Search[/topic], [topic="67095"]Swiss File Knife GUI[/topic], [topic="69072"]Mouse Location Pointer[/topic], [topic="86040"]Standard Deviation Calculator[/topic]

Link to comment
Share on other sites

Thanks to all for the attention to this, however, neither solution will work. Maybe I should have expressed the limitations with this. The date input cannot be expressed in any other format, other than the way it currently is expressed. Meaning, the date is taken from a Windows Control and must be expressed as DD/MM/YYYY. It must be inputted and outputted as DD/MM/YYYY. Any other solutions available to this?

Thanks.

Use the format function I gave above to format the date so it's usable with the UDFs and then pass through the script with the new format. Formatting is the easy part. You'll see.

If you give me your input control code, I'll show you how to do it.

Edited by Ealric

My Projects: [topic="89413"]GoogleHack Search[/topic], [topic="67095"]Swiss File Knife GUI[/topic], [topic="69072"]Mouse Location Pointer[/topic], [topic="86040"]Standard Deviation Calculator[/topic]

Link to comment
Share on other sites

You don't have to save the results of the stringsplit as individual variables. This should add 4 months to the date

#include <date.au3>
$aVal = 4
$aType = "M"
$aDate = ""
$dtArray = StringSplit("29/02/2008", "/")
For $I = 3 To 1 Step -1
   $aDate &= $dtArray[$I] & "/"
Next
$date = _DateAdd($aType, $aVal, StringTrimRight($aDate, 1))
MsgBox(0, "Test", $date)

Edit: Forgot the code tags

Edited by GEOSoft

George

Question about decompiling code? Read the decompiling FAQ and don't bother posting the question in the forums.

Be sure to read and follow the forum rules. -AKA the AutoIt Reading and Comprehension Skills test.***

The PCRE (Regular Expression) ToolKit for AutoIT - (Updated Oct 20, 2011 ver:3.0.1.13) - Please update your current version before filing any bug reports. The installer now includes both 32 and 64 bit versions. No change in version number.

Visit my Blog .. currently not active but it will soon be resplendent with news and views. Also please remove any links you may have to my website. it is soon to be closed and replaced with something else.

"Old age and treachery will always overcome youth and skill!"

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