Jump to content

Date Calculations (M-F)


Davarious
 Share

Recommended Posts

Just some date functions I was working on for a report, my format requires MM-DD-YYYY, for filenames as well as for the report engine I am working with, customize as needed. Hope its helpful.

#include <Date.au3>
Func _LastBusDay() ; This is a date function used here only, hence why it is down here, instead of above
    If _DateToDayOfWeek(@YEAR, @MON, @MDAY) = 2 Then
        $iSubtractValue = -3
    Else
        $iSubtractValue = -1
    EndIf   
    $sLastBusDay = _DateAdd('d',$iSubtractValue,_NowCalc()) ; Format is "YYYY/MM/DD HH:MM:SS" We need to change this!
    $sLastBusDay = StringLeft($sLastBusDay,10) ; Strip the time off, makes it easier to work with.
    $sLastBusDayM = StringLeft(StringRight($sLastBusDay,5),2) ; Breaking it down!
    $sLastBusDayD = StringRight($sLastBusDay,2) ; Breaking it down!
    $sLastBusDayY = StringLeft($sLastBusDay,4) ; Breaking it down!
    $sLastBusDay = $sLastBusDayM & "-" & $sLastBusDayD & "-" & $sLastBusDayY
    Return $sLastBusDay
EndFunc

Func _Yesterday() ; This function results in yesterdays date.
    $sYesterday = _DateAdd( 'd',-1,_NowCalc()) ; Format is "YYYY/MM/DD HH:MM:SS" We need to change this!
    $sYesterday = StringLeft($sYesterday,10) ; Strip the time off, makes it easier to work with.
    $sYesterdayM = StringLeft(StringRight($sYesterday,5),2) ; Breaking it down!
    $sYesterdayD = StringRight($sYesterday,2) ; Breaking it down!
    $sYesterdayY = StringLeft($sYesterday,4) ; Breaking it down!
    $sYesterday = $sYesterdayM & "-" & $sYesterdayD & "-" & $sYesterdayY
    Return $sYesterday
EndFunc

Func _DateWeekAgo() ; This function calculates the result of the date 1 week prior.
    $sWeekAgo = _DateAdd('d', -7, _NowCalc()) ; Format is "YYYY/MM/DD HH:MM:SS" We need to change this!
    $sWeekAgo = StringLeft($sWeekAgo,10) ; Strip the time off, makes it easier to work with.
    $sWeekAgoM = StringLeft(StringRight($sWeekAgo,5),2) ; Breaking it down!
    $sWeekAgoD = StringRight($sWeekAgo,2) ; Breaking it down!
    $sWeekAgoY = StringLeft($sWeekAgo,4) ; Breaking it down!
    $sWeekAgo = $sWeekAgoM & "-" & $sWeekAgoD & "-" & $sWeekAgoY
    Return $sWeekAgo
EndFunc

Func _FirstOfMonth() ; This function results in the first day of the month 
    Return (@MON & "-" & "01" & "-" & @YEAR)
EndFunc

Func _LastOfMonth() ; This function results in the last day of the month 
    Return (@MON & "-" & _DateDaysInMonth(@YEAR, @MON) & "-" & @YEAR)
EndFunc

Func _Today() ;Return the current date in mm-dd-yy format so it can be used in filenames
    Return (@MON & "-" & @MDAY & "-" & @YEAR)
EndFunc
Edited by Davarious
Link to comment
Share on other sites

I just thought I would share this, since I am working on a big report automation program that takes advantage of this...

#include <Date.au3>
Func _LastBusDay()
    If _DateToDayOfWeek(@YEAR, @MON, @MDAY) = 2 Then
        $iSubtractValue = -3
    Else
        $iSubtractValue = -1
    EndIf   
    $sLastBusDay = _DateAdd( 'd',$iSubtractValue,_NowCalc()); Format is "YYYY/MM/DD HH:MM:SS" We need to change this if you are to inject into a filename!
    $sLastBusDay = StringLeft($sLastBusDay,10); Strip the time off, makes it easier to work with.
    $sLastBusDayM = StringLeft(StringRight($sLastBusDay,5),2); Breaking it down!
    $sLastBusDayD = StringRight($sLastBusDay,2); Breaking it down!
    $sLastBusDayY = StringLeft($sLastBusDay,4); Breaking it down!
    $sLastBusDay = $sLastBusDayM & "-" & $sLastBusDayD & "-" & $sLastBusDayY
; Uncomment next line to produce a msg box, otherwise the script returns MM-DD-YYYY which is usable in file names, and a common format for dates.
; MsgBox( 4096, "", "Date: " & $sLastBusDay )
EndFunc
If it's a report automation program maybe you should allow for it to run on a Sunday.
Serial port communications UDF Includes functions for binary transmission and reception.printing UDF Useful for graphs, forms, labels, reports etc.Add User Call Tips to SciTE for functions in UDFs not included with AutoIt and for your own scripts.Functions with parameters in OnEvent mode and for Hot Keys One function replaces GuiSetOnEvent, GuiCtrlSetOnEvent and HotKeySet.UDF IsConnected2 for notification of status of connected state of many urls or IPs, without slowing the script.
Link to comment
Share on other sites

Just a comment about the actual numbers used in the title of the file.

I have been doing this sort of thing for years and it is easier to have the left numbers change the least and the right numbers change the most. this makes searching by yr-m-d much more efficient.

that would be

$sLastBusDay = $sLastBusDayY & "-" & $sLastBusDayM & "-" & $sLastBusDayD

example my way

2007-10-15

2005-09-22

2008-01-05

That can be sorted chronologically very easily

Your way can not be completed as easy

15-10-2007

22-09-2005

05-01-2008

Hope that helps futuristically!!!

8)

Edited by Valuater

NEWHeader1.png

Link to comment
Share on other sites

Using different aspects for that.

We have some customers who shut down on Sunday, and some who run. An INI file contains these settings on the remote machine and determine what range to run for that specific customer.

I just figured this is a neat function to share.

Example, I have a _Yesterday() function calculating Sunday, but the _LastBusDay() calculates the friday, so I can capture the report range our customers want in the weekend period, but with a little more control, and less conditional statements throughout the code.

Link to comment
Share on other sites

I would not disagree for filenames, unfortunately we have a report engine that exists in the software I am working on this to control, and that report engine requires the range typed in MM-DD-YYYY to generate the report file. This function will actually be used with SEND("") as well, hence why I have it in this format.

Just a comment about the actual numbers used in the title of the file.

I have been doing this sort of thing for years and it is easier to have the left numbers change the least and the right numbers change the most. this makes searching by yr-m-d much more efficient.

that would be

$sLastBusDay = $sLastBusDayY & "-" & $sLastBusDayM & "-" & $sLastBusDayD

example my way

2007-10-15

2005-09-22

2008-01-05

That can be sorted chronologically very easily

Your way can not be completed as easy

15-10-2007

22-09-2005

05-01-2008

Hope that helps futuristicly!!

8)

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