How to? Week, Month, Quarter

  • This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn more.
May 20, 2008
424
2
#1
These are aliases for determining the current quarter, or the start/end of the week, month, or quarter. They are loaded from my aliases file. They are also wrapped with @execstr and loaded from my functions file.
Code:
:get the Mon-Sun start date of the current week or specified date
 WeekStart=` & ^
    setlocal & ^
    set Arg=%1$ & ^
    set d=%@date[%@if[%@len[%Arg] GT 0,%Arg,%_date]] & ^
    do while %@dowi[%d] NE 2 (set d=%@dec[%d]) & ^
    echo %@makedate[%d] & ^
    endlocal`

:get the Mon-Sun end date of the current week or specified date
 WeekEnd=` & ^
    setlocal & ^
    set Arg=%1$ & ^
    set d=%@date[%@if[%@len[%Arg] GT 0,%Arg,%_date]] & ^
    do while %@dowi[%d] NE 1 (set d=%@inc[%d]) & ^
    echo %@makedate[%d] & ^
    endlocal`

:get the first day of the current month or specified date
 MonthStart=` & ^
    setlocal & ^
    set Arg=%1$ & ^
    iff %@len[%Arg] GT 0 then & ^
        echo %@makedate[%@date[%@month[%Arg]/1/%@year[%Arg]]] & ^
    else & ^
        echo %@makedate[%@date[%_month/1/%_year]] & ^
    endiff & ^
    endlocal`

:get the last day of the current month or specified date
 MonthEnd=` & ^
    setlocal & ^
    set Arg=%1$ & ^
    set d=%@date[%@if[%@len[%Arg] GT 0,%Arg,%_date]] & ^
    set dm=%@month[%@makedate[%d]] & ^
    do while %@month[%@makedate[%d]] EQ %dm (set d=%@inc[%d]) & ^
    set d=%@dec[%d] & ^
    echo %@makedate[%d] & ^
    endlocal`

:get quarter given date or month arg
 GetQuarter=` & ^
    setlocal & ^
    set Arg=%1$ & ^
    iff %@fields["/",%[Arg]] GT 1 .OR. %@fields["/",%[Arg]] GT 1 then & ^
        set Quarter=%@eval[%@floor[%@eval[(%@month[%[Arg]] - 1) / 3]] + 1] & ^
    else & ^
        set Quarter=%@eval[%@floor[%@eval[(%[Arg] - 1) / 3]] + 1] & ^
    endiff & ^
    echo %Quarter & ^
    endlocal`
   
:get start month of quarter given date or month arg
 GetQuarterStart=` & ^
    setlocal & ^
    set Arg=%1$ & ^
    iff %@fields["/",%[Arg]] GT 1 .OR. %@fields["/",%[Arg]] GT 1 then & ^
        set Month=%@month[%[Arg]] & ^
    else & ^
        set Month=%[Arg] & ^
    endiff & ^
    set Quarter=%@eval[%@floor[%@eval[(%[Month] - 1) / 3]] + 1] & ^
    do while %[Month] GT 1 .AND. %Quarter EQ %@eval[%@floor[%@eval[(%@dec[%[Month]] - 1) / 3]] + 1] (set Month=%@dec[%[Month]]) & ^
    echo %Month & ^
    endlocal`

:get end month of quarter given date or month arg
 GetQuarterEnd=` & ^
    setlocal & ^
    set Arg=%1$ & ^
    iff %@fields["/",%[Arg]] GT 1 .OR. %@fields["/",%[Arg]] GT 1 then & ^
        set Month=%@month[%[Arg]] & ^
    else & ^
        set Month=%[Arg] & ^
    endiff & ^
    set Quarter=%@eval[%@floor[%@eval[(%[Month] - 1) / 3]] + 1] & ^
    do while %[Month] LT 12 .AND. %Quarter EQ %@eval[%@floor[%@eval[(%@inc[%[Month]] - 1) / 3]] + 1] (set Month=%@inc[%[Month]]) & ^
    echo %Month & ^
    endlocal`