Sign in to follow this  
Followers 0
kpu

Subtract from Date

8 posts in this topic

I'm trying to subtact One day from the current day and list it in the MM/DD/YYYY.

If I do:

$oldFile = @MON & "-" & @MDAY  -1 & "-" & @YEAR & ".txt"
MsgBox(32,"Oldfile",$oldFile)

If todays date is 02/02/2006 file would look like 02/02/2006.txt

When I subtract it goes 02-1-2000.txt and I need it to display 02-01-2006.txt

Am I missing something? Or would it just be best to use the default yyyy/MM/DD format used by _Dateadd.

Share this post


Link to post
Share on other sites



#2 ·  Posted (edited)

I'm trying to subtact One day from the current day and list it in the MM/DD/YYYY.

If I do:

$oldFile = @MON & "-" & @MDAY  -1 & "-" & @YEAR & ".txt"
MsgBox(32,"Oldfile",$oldFile)

If todays date is 02/02/2006 file would look like 02/02/2006.txt

When I subtract it goes 02-1-2000.txt and I need it to display 02-01-2006.txt

Am I missing something? Or would it just be best to use the default yyyy/MM/DD format used by _Dateadd.

its close to one of the examples in the helpfile for _DateAdd() :

#include <Date.au3>

$sNewDate = _DateAdd( 'd',-1, _NowCalcDate())
MsgBox( 4096, "", "Today -1 day:" & $sNewDate )
Edited by JdeB

Visit the SciTE4AutoIt3 Download page for the latest versions        Beta files                                                          Forum Rules
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Share this post


Link to post
Share on other sites

You are correct, but again. I would like it in the MM/DD/YYYY format. I don't really want to rewrite the date.au3 file is what I'm getting at.

its close to one of the examples in the helpfile for _DateAdd() :

#include <Date.au3>

$sNewDate = _DateAdd( 'd',-1, _NowCalcDate())
MsgBox( 4096, "", "Today -1 day:" & $sNewDate )

Share this post


Link to post
Share on other sites

#4 ·  Posted (edited)

You are correct, but again. I would like it in the MM/DD/YYYY format. I don't really want to rewrite the date.au3 file is what I'm getting at.

Does DateTimeFormat() return the correct version for you ?

#include <Date.au3>
$sNewDate =_DateTimeFormat(_dateAdd( 'd',-1, _NowCalcDate()),2)
MsgBox( 4096, "", "Today -1 day:" & $sNewDate )
Edited by JdeB

Visit the SciTE4AutoIt3 Download page for the latest versions        Beta files                                                          Forum Rules
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Share this post


Link to post
Share on other sites

$oldFile = @MON & "-" & @MDAY  -1 & "-" & @YEAR & ".txt"
MsgBox(32,"Oldfile",$oldFile)
Here's a StringRegExpReplace I just whipped up that should do the trick:

$oldFile = @MON & "-" & @MDAY  -1 & "-" & @YEAR & ".txt"
MsgBox(0,"Info",$oldFile& @CRLF &StringRegExpReplace($oldFile, "([0-9]{2}[\-])([0-9]{1}[\-][0-9]{4})", "\10\2"))

That's the ticket!!


[u]My UDFs[/u]Coroutine Multithreading UDF LibraryStringRegExp GuideRandom EncryptorArrayToDisplayString"The Brain, expecting disaster, fails to find the obvious solution." -- neogia

Share this post


Link to post
Share on other sites

You Rock! That is exactly what I need! I don' t exactly what your code is doing, but it works and that's all that matters. :lmao:

Thanks Again!

Here's a StringRegExpReplace I just whipped up that should do the trick:

$oldFile = @MON & "-" & @MDAY  -1 & "-" & @YEAR & ".txt"
MsgBox(0,"Info",$oldFile& @CRLF &StringRegExpReplace($oldFile, "([0-9]{2}[\-])([0-9]{1}[\-][0-9]{4})", "\10\2"))

That's the ticket!!

Share this post


Link to post
Share on other sites

You Rock! That is exactly what I need! I don' t exactly what your code is doing, but it works and that's all that matters. :lmao:

Thanks Again!

Well, thank you. I can make a quick explanation of my code, I've got time to kill. Regular expressions confused me for a long time.

So this is the pattern string: "([0-9]{2}[\-])([0-9]{1}[\-][0-9]{4})"

Let's start from the outside in. Anything contained by parentheses like "(...)" is called a capture; anything that matches what's inside is stored as "\n" where n is the number of the capture. This is used later in the replace pattern.

Inside the first capture, or \1 for reference, we have: "[0-9]{2}[\-]" What this tells the function to do is search the presented string ("02-2-2006") for a digit from 0 to 9 ("[0-9]") and to look for 2 of them in a row ("{2}"), then look for a dash ("[\-]", the backslash is there because a dash is a special character, you wouldn't want the function to interpret the dash as it would in "[0-9]" or the function would look for a character on either side of the dash). So inside capture 1, the function will store the first 3 characters of our presented string ("02-") because they match the search criteria.

In the second capture, we look for the rest of the string. Here we search for a single digit instead of 2, because we don't want to tack a 0 in front of a 10, 11, or 12 ("01-011-2006"). Then it searches for another dash and 4 more digits ("[\-][0-9]{4}"). The second capture stores "2-2006" with our presented string. Now we have to replace what the pattern matched, which ended up being the whole string, with what we want it to look like.

Now, the key here is to remember that captures are in order from left to right (\1,\2,\3...\9) up to nine captures. You can picture these as variables, such as:

\1 = "02-"

\2 = "2-2006"

Now in our replace pattern, we find "\10\2". This says "stick a 0 in between \1 and \2". The parser doesn't need a separater between \1 and 0 because with only 9 captures, there are no double digits after the "\".

Test string: "02-2-2006"

Pattern string: "([0-9]{2}[\-])([0-9]{1}[\-][0-9]{4})"

Test string: "02-2-2006"

Pattern string: "([0-9]{2}[\-])([0-9]{1}[\-][0-9]{4})"

Replace string: "\10\2"

Unfortunately, this quick explanation only scratches the surface... as they say: "Learn by doing."


[u]My UDFs[/u]Coroutine Multithreading UDF LibraryStringRegExp GuideRandom EncryptorArrayToDisplayString"The Brain, expecting disaster, fails to find the obvious solution." -- neogia

Share this post


Link to post
Share on other sites

#8 ·  Posted (edited)

$oldFile = @MON & "-" & StringFormat("%02d", @MDAY - 1) & "-" & @YEAR & ".txt"
MsgBox(32, "Oldfile", $oldFile)

This could be a nice option for StringFormat.

$sNewDate = _DateAdd( 'd',-1, _NowCalcDate())

could be the better code when today is the first day of a new month...

Edited by Geert

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
Sign in to follow this  
Followers 0