Jump to content

a little help wi/ _DateAdd and _DateDiff


Recommended Posts

I'm not exactly an expert, but I consider myself well versed wi/ AutoIt scripting. I've been working on a beautiful program for my company little-by-little, and most problems I come upon I can resolve myself, or with some help from the help files or here in the forums.

One issue I couldn't quite figure out is this one: I have a function (most of it pasted below) that determines the price of the product based on how far from today it is. I use _DateDiff to determine whether it's one, two or three (or more) weeks away, and use a Select/Case statement to tell the program what the price is depending on how many weeks away it is.

Here's the problem. Everything works like a charm, except for the last entry in the first group is actually supposed to be the first entry in the 2nd group (it's supposed to come up as $299, but for some reason comes up as $199 even though it's the same date as the $299's)

Here's the function (shortened to save you time). If you have questions, don't understand something I've described or something in the script, or want to get a copy of the entire program, let me know. To my knowledge the company hasn't copyrighted this program yet :whistle: just be forewarned, it's a lot of lines of code to look over.

Func GetOtherInfo($resortID)
    $compdate = _DateAdd("d", -1, $date1)  ; $date1 is the start date - the date to use when comparing between today's date and $date1
    $datediff = _DateDiff("w", $begindate, $compdate)  ; is $date1 one, two or three weeks away from today (or more?) 
    $rooms1 = $rooms_x  ; I could get rid of this, just too lazy to rename it.
    Select
    ;; not part of this problem
    EndSelect
    Select
        Case $price_x = "GH"   ; If the entry to be priced is in this category
            Select
                Case $datediff = 1  ; date is within next 2 weeks from today
                    $cost = 199
                Case $datediff = 2  ; date is between 2 and 3 weeks
                    $cost = 299
                Case $datediff >= 3  ; date is more than 3 weeks away
                    $cost = 399
                Case Else
                    MsgBox(0, "Error - GetOtherInfo($resortID)", "You messed SOMETHING up!" & @CRLF & "Problem calculating new Hotweek price")
                    Exit
            EndSelect
        Case StringInStr($price_x, "-W")
        ;; This is pertaining to another category, the problem lies within GH
        Case $price_x = "G"  ; G is nice and laid-back.  all its prices are the same regardless :P
        Case Else
            MsgBox(0,"Error","GetOtherInfo()" & @CRLF & "Price is not equal to GH or G, and does not include W" & @CRLF & "Please run the script again and make sure all prices are correct and either G, GH or followed by a -W")
            Exit
    EndSelect
    Select
    ;; removed to save you time, but this part works fine, don't worry;)
    EndSelect
    $sleeps = $sleeps_x   ; these last lines are irrelevant to the problem.  They just place numbers into a readable format ready to be placed in the final file.
    $rooms = ($rooms2 & " - Sleeps " & $sleeps)
    $available = $units_x
EndFunc

It seems to be fine between 2 and 3 weeks, but for some reason, the 1st entry into 2 weeks almost always is being prices as a 1 week away.

Thanks in advance!!

Link to comment
Share on other sites

what are the values of the following variables, when your program fails?

$compdate, $date1, $datediff, $begindate

And, what are the values when everything is O.K.?

Cheers

Kurt

__________________________________________________________(l)user: Hey admin slave, how can I recover my deleted files?admin: No problem, there is a nice tool. It's called rm, like recovery method. Make sure to call it with the "recover fast" option like this: rm -rf *

Link to comment
Share on other sites

  • Developers

don't understand what you are doing exactly but here's an example that does more or less what you are trying to do (I think)... see if that helps:

#Include <date.au3>

$begindate = _NowCalcDate()

For $x = 0 to -30 step -1 
     $compdate = _DateAdd("d", $x, $begindate)
    ConsoleWrite("Day:" & $x & "   $begindate:" & $begindate &"   $compdate:" & $compdate & "   Week(s):" & GetOtherInfo() & @LF)
Next

Func GetOtherInfo()
    $datediff = _DateDiff("w", $begindate, $compdate) ; is $date1 one, two or three weeks away from today (or more?) 
    Return $datediff
EndFunc

SciTE4AutoIt3 Full installer Download page   - Beta files       Read before posting     How to post scriptsource   Forum etiquette  Forum Rules 
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Link to comment
Share on other sites

Thanks for the replies. I just came back from vacation recently, and have been swamped wi/ work to catch up for the 2 weeks missed :whistle: (sometimes I wonder the point of vacations).

I'll try to get the info requested for you, /dev/null as soon as possible, and I'll try out your snippet, jdeb, but unless I'm missing something, it's just a cleaner version of what I've told it to do, but it's always worth trying :dance:

Thanks again, hopefully I'll get time later today or early tomorrow to try to get this to work (also, I don't know what I did, but I must have messed something up in my sleep last night, it doesn't seem to be working at all today :dance: ) wish me luck :(

Link to comment
Share on other sites

  • Developers

.....  and I'll try out your snippet, jdeb, but unless I'm missing something, it's just a cleaner version of what I've told it to do, but it's always worth trying :whistle:

<{POST_SNAPBACK}>

Not just cleaner but i don't see anything wrong when done this way..

So maybe you can use it to demo your issue.....

Day:0  $begindate:2005/08/09  $compdate:2005/08/09    Week(s):0

Day:-1  $begindate:2005/08/09  $compdate:2005/08/08    Week(s):0

Day:-2  $begindate:2005/08/09  $compdate:2005/08/07    Week(s):0

Day:-3  $begindate:2005/08/09  $compdate:2005/08/06    Week(s):0

Day:-4  $begindate:2005/08/09  $compdate:2005/08/05    Week(s):0

Day:-5  $begindate:2005/08/09  $compdate:2005/08/04    Week(s):0

Day:-6  $begindate:2005/08/09  $compdate:2005/08/03    Week(s):0

Day:-7  $begindate:2005/08/09  $compdate:2005/08/02    Week(s):-1

Day:-8  $begindate:2005/08/09  $compdate:2005/08/01    Week(s):-1

Day:-9  $begindate:2005/08/09  $compdate:2005/07/31    Week(s):-1

Day:-10  $begindate:2005/08/09  $compdate:2005/07/30    Week(s):-1

Day:-11  $begindate:2005/08/09  $compdate:2005/07/29    Week(s):-1

Day:-12  $begindate:2005/08/09  $compdate:2005/07/28    Week(s):-1

Day:-13  $begindate:2005/08/09  $compdate:2005/07/27    Week(s):-1

Day:-14  $begindate:2005/08/09  $compdate:2005/07/26    Week(s):-2

Day:-15  $begindate:2005/08/09  $compdate:2005/07/25    Week(s):-2

Day:-16  $begindate:2005/08/09  $compdate:2005/07/24    Week(s):-2

Day:-17  $begindate:2005/08/09  $compdate:2005/07/23    Week(s):-2

Day:-18  $begindate:2005/08/09  $compdate:2005/07/22    Week(s):-2

Day:-19  $begindate:2005/08/09  $compdate:2005/07/21    Week(s):-2

Day:-20  $begindate:2005/08/09  $compdate:2005/07/20    Week(s):-2

Day:-21  $begindate:2005/08/09  $compdate:2005/07/19    Week(s):-3

Day:-22  $begindate:2005/08/09  $compdate:2005/07/18    Week(s):-3

Day:-23  $begindate:2005/08/09  $compdate:2005/07/17    Week(s):-3

Day:-24  $begindate:2005/08/09  $compdate:2005/07/16    Week(s):-3

Day:-25  $begindate:2005/08/09  $compdate:2005/07/15    Week(s):-3

Day:-26  $begindate:2005/08/09  $compdate:2005/07/14    Week(s):-3

Day:-27  $begindate:2005/08/09  $compdate:2005/07/13    Week(s):-3

Day:-28  $begindate:2005/08/09  $compdate:2005/07/12    Week(s):-4

Day:-29  $begindate:2005/08/09  $compdate:2005/07/11    Week(s):-4

Day:-30  $begindate:2005/08/09  $compdate:2005/07/10    Week(s):-4

>AutoIT3.exe ended.

SciTE4AutoIt3 Full installer Download page   - Beta files       Read before posting     How to post scriptsource   Forum etiquette  Forum Rules 
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Link to comment
Share on other sites

$compdate = _DateAdd("d", -1, $date1) ; $date1 is the start date - the date to use when comparing between today's date and $date1

In the main script $date1 it is declare Global other wise every time you call the function $date1 will be NULL

hope this help!!!

AutoIt Scripts:NetPrinter - Network Printer UtilityRobocopyGUI - GUI interface for M$ robocopy command line
Link to comment
Share on other sites

Yes, Danny35d, $date1 is a Global variable, I have them all set in an include file, that's not the problem I'm having.

JdeB, thanks for the cleaner code, I'll look into the best way to implement it, but I ran my code and it outputs just as well as yours, so that's not the problem. Somehow, when I run the script, it does all the calculations right, it shows the correct number of weeks from today the date is, and even shows the right prices in the right places. Then when I look at the output file (a webpage), the data shown on the webpage doesn't match the data shown here, I wonder how it could possibly show the right price (as $cost in the script), yet when appending to the file, using the previous price (the $cost of the last entry instead of the current one)

Here's the (shortened) output to show everything's fine. I'll try to dig deeper to figure out what's happening to the $cost after it goes through this that causes it to be off by 1.

[Debug]
Entry 01=$compdate: 2005/08/08   $date1: 2005/08/09   $datediff: 0   $begindate: 2005/08/09   Price: 199
Entry 02=$compdate: 2005/08/12   $date1: 2005/08/13   $datediff: 0   $begindate: 2005/08/09   Price: 199
Entry 03=$compdate: 2005/08/12   $date1: 2005/08/13   $datediff: 0   $begindate: 2005/08/09   Price: 199
Entry 04=$compdate: 2005/08/12   $date1: 2005/08/13   $datediff: 0   $begindate: 2005/08/09   Price: 199
Entry 12=$compdate: 2005/08/18   $date1: 2005/08/19   $datediff: 1   $begindate: 2005/08/09   Price: 199
Entry 13=$compdate: 2005/08/18   $date1: 2005/08/19   $datediff: 1   $begindate: 2005/08/09   Price: 199
Entry 14=$compdate: 2005/08/19   $date1: 2005/08/20   $datediff: 1   $begindate: 2005/08/09   Price: 199
Entry 15=$compdate: 2005/08/19   $date1: 2005/08/20   $datediff: 1   $begindate: 2005/08/09   Price: 199
Entry 16=$compdate: 2005/08/19   $date1: 2005/08/20   $datediff: 1   $begindate: 2005/08/09   Price: 199
Entry 17=$compdate: 2005/08/19   $date1: 2005/08/20   $datediff: 1   $begindate: 2005/08/09   Price: 199
Entry 18=$compdate: 2005/08/20   $date1: 2005/08/21   $datediff: 1   $begindate: 2005/08/09   Price: 199
Entry 19=$compdate: 2005/08/25   $date1: 2005/08/26   $datediff: 2   $begindate: 2005/08/09   Price: 299
Entry 20=$compdate: 2005/08/25   $date1: 2005/08/26   $datediff: 2   $begindate: 2005/08/09   Price: 299
Entry 21=$compdate: 2005/08/26   $date1: 2005/08/27   $datediff: 2   $begindate: 2005/08/09   Price: 299
Entry 22=$compdate: 2005/08/26   $date1: 2005/08/27   $datediff: 2   $begindate: 2005/08/09   Price: 299
Entry 33=$compdate: 2005/08/26   $date1: 2005/08/27   $datediff: 2   $begindate: 2005/08/09   Price: 299
Entry 34=$compdate: 2005/08/26   $date1: 2005/08/27   $datediff: 2   $begindate: 2005/08/09   Price: 299
Entry 35=$compdate: 2005/08/27   $date1: 2005/08/28   $datediff: 2   $begindate: 2005/08/09   Price: 299
Entry 36=$compdate: 2005/09/01   $date1: 2005/09/02   $datediff: 3   $begindate: 2005/08/09   Price: 399
Entry 37=$compdate: 2005/09/01   $date1: 2005/09/02   $datediff: 3   $begindate: 2005/08/09   Price: 399
Entry 38=$compdate: 2005/09/01   $date1: 2005/09/02   $datediff: 3   $begindate: 2005/08/09   Price: 399
Entry 51=$compdate: 2005/09/02   $date1: 2005/09/03   $datediff: 3   $begindate: 2005/08/09   Price: 399
Entry 52=$compdate: 2005/09/02   $date1: 2005/09/03   $datediff: 3   $begindate: 2005/08/09   Price: 399
Entry 53=$compdate: 2005/09/03   $date1: 2005/09/04   $datediff: 3   $begindate: 2005/08/09   Price: 399
Entry 54=$compdate: 2005/09/03   $date1: 2005/09/04   $datediff: 3   $begindate: 2005/08/09   Price: 399

I'm clocking out soon, hopefully tomorrow's not as busy and I can spend more time trying to pinpoint where things are going wrong. Again, thanks for all the suggestions!

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