Jump to content

Previous Day's Date


gcriaco
 Share

Recommended Posts

;====================================================

;

; Description: Returns the previous day's date

; Syntax: _DateLastDay( $DateIn, $Delimiter )

; Parameter(s): $DateIn - Input date

; $Delimiter - Delimiter of Days, Month and Year

; Requirement(s): Date is in format DD/MM/YYYY, where "/" is a variable delimiter

; You need to include Date.au3 (#include <Date.au3>)

; Return Value(s): On Success - Returns the previous day's date

; On Failure - Returns 0 and sets @error = 1

; Author(s): Giuseppe Criaco <gcriaco@quipo.it>

;

;====================================================

Func _DateLastDay($DateIn, $Delimiter)

$ArrDate = StringSplit($DateIn, $Delimiter)

$DD = $ArrDate[1]

$MM = $ArrDate[2]

$YYYY = $ArrDate[3]

If $ArrDate[0] <> 3 Then

SetError( 1 )

Return 0

Exit

EndIf

If _DateIsValidMonthNum( $MM ) And _DateIsValidYear( $YYYY ) Then

If $DD > 1 Then ;Day > 1

$DD = $DD - 1

ElseIf $MM > 1 Then ;Month > 1

$MM = $MM -1

Select

Case $MM = 02

If _DateIsLeapYear( $YYYY ) = 1 Then

$DD = 29

Else

$DD = 28

Endif

Case $MM = 04 OR $MM = 06 OR $MM = 09 OR $MM = 11

$DD = 30

Case Else

$DD = 31

EndSelect

Else

$DD = 31

$MM = 12

$YYYY = $YYYY -1

EndIf

Else

SetError( 1 )

Return 0

Exit

EndIf

$DD = StringFormat( "%02d", $DD )

$MM = StringFormat( "%02d", $MM )

$DateOut = $DD & $Delimiter & $MM & $Delimiter & $YYYY

Return $DateOut

EndFunc

Link to comment
Share on other sites

  • Developers

How did you use it ???

this seems to work fine:

#include <date.au3>
msgbox(0, 'test',_DateLastDay(@MDAY & "/" & @MON & "/" &@YEAR ,"/"))
exit

;====================================================
;
; Description: Returns the previous day's date
; Syntax: _DateLastDay( $DateIn, $Delimiter )
; Parameter(s): $DateIn - Input date
; $Delimiter - Delimiter of Days, Month and Year
; Requirement(s): Date is in format DD/MM/YYYY, where "/" is a variable delimiter
; You need to include Date.au3 (#include <Date.au3>)
; Return Value(s): On Success - Returns the previous day's date
; On Failure - Returns 0 and sets @error = 1
; Author(s): Giuseppe Criaco <gcriaco@quipo.it>
;
;====================================================

Func _DateLastDay($DATEIN, $DELIMITER)
   $ARRDATE = StringSplit($DATEIN, $DELIMITER)
   
   $DD = $ARRDATE[1]
   $MM = $ARRDATE[2] 
   $YYYY = $ARRDATE[3]
   
   If $ARRDATE[0] <> 3 Then
      SetError(1)
      Return 0
      Exit
   EndIf 
   
   If _DateIsValidMonthNum ($MM) And _DateIsValidYear ($YYYY) Then
      
      If $DD > 1 Then;Day > 1 
         $DD = $DD - 1
      ElseIf $MM > 1 Then;Month > 1
         $MM = $MM - 1
         
         Select
            Case $MM = 02
               If _DateIsLeapYear($YYYY) = 1 Then
                  $DD = 29
               Else
                  $DD = 28
               EndIf
            Case $MM = 04 Or $MM = 06 Or $MM = 09 Or $MM = 11
               $DD = 30
            Case Else
               $DD = 31 
         EndSelect
      Else 
         $DD = 31
         $MM = 12
         $YYYY = $YYYY - 1
      EndIf
      
   Else
      SetError(1)
      Return 0
      Exit
   EndIf
   
   $DD = StringFormat( "%02d", $DD)
   $MM = StringFormat( "%02d", $MM)
   
   $DATEOUT = $DD & $DELIMITER & $MM & $DELIMITER & $YYYY
   
   Return $DATEOUT
EndFunc  ;==>_DateLastDay

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

  • Developers

OK, that works.  Stupid question:

How do I get it to format the date as MM/DD/YYYY ?? :-)

EDIT:

just change this portion:

$MM = $ARRDATE[1]

$DD = $ARRDATE[2]

$YYYY = $ARRDATE[3]

Edited by JdeB

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

OK works great now ... was trying to get the value from $DateOut but it was saying the variable had not been defined

You can also change the output format here:

$DATEOUT = $DD & $DELIMITER & $MM & $DELIMITER & $YYYY

I needed it to show date in the following fomat:

$DATEOUT = $YYYY & $MM & $DD

So it shows as 20040520 (without delimiters)

Very nice UDF though ... I'll definetely be using this one

:D

We have enough youth. How about a fountain of SMART?

Link to comment
Share on other sites

  • 4 months later...

Here's a simpler version for calculating yesterday's date:

#include <date.au3>
$YYYY = @YEAR
$MM = @MON
$DD = @MDAY
If $DD > 1 Then
      $DD = $DD - 1
ElseIf $MM > 1 Then
      $MM = $MM - 1 
      $DD = _DateDaysInMonth ($MM, $YYYY)
Else
      $DD = 31
      $MM = 12
      $YYYY = $YYYY - 1
EndIf

$YesterdayDate = StringFormat("%02d/%02d/%04d", $DD, $MM, $YYYY)
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...