Last workday of month as IF/ELSE

Discuss RoboTask here
Post Reply
Rukbunker
Posts: 192
Joined: Mon Feb 22, 2016 4:06 pm
Location: Netherlands

Last workday of month as IF/ELSE

Post by Rukbunker »

Hey Oleg,

In this topic, you explained to set a "schedule" on the last workday of the month. Now this is very nice as trigger, but I want to use this "function" in a task that runs daily.

This task should execute something when it's the last working day of the month, so it's not a trigger of schedule function, more a "if/then/else" function. In the past, you have created a script for me which counts the number of workdays.

Code: Select all

function getworkday(d,m,y)
{
  workday = 0;
  m--; // month begins from 0 in Javascript
  dt = new Date(y, m, d);

  weekday = dt.getDay();
  // 0 Sunday
  // 1 Monday
  // 2 Tuesday
  // 3 Wednesday
  // 4 Thursday
  // 5 Friday
  // 6 Saturday
  for (i=d; i>0; i--)
    {
      if ((weekday != 6) && (weekday != 0))
        {
          workday++;
        }
        weekday--;
        if (weekday <0)
          {weekday = 6;
          }
    }
  return workday;
}

//LogMessage(getworkday(7,1,2016));
I found on Google the next script:

Code: Select all

function lastBusinessDayOfMonth(year, month) {
    var date = new Date();
    var offset = 0;
    var result = null;
    
    if ('undefined' === typeof year || null === year) {
        year = date.getFullYear();
    }
    
    if ('undefined' === typeof month || null === month) {
        month = date.getMonth();
    }

    do {
        result = new Date(year, month, offset);
        
        offset--;
    } while (0 === result.getDay() || 6 === result.getDay());

    return result;
}
However, the output is like this (and is correct by the way) (more here)

Code: Select all

Tue May 31 00:00:00 UTC+0200 2018
I've tried this code, but it's more dedicated for websites becuase of the alert function.

Code: Select all

function lastBusinessDayOfMonth(year, month) {
    var date = new Date();
    var offset = 0;
    var result = null;
    
    if ('undefined' === typeof year || null === year) {
        year = date.getFullYear();
    }
    
    if ('undefined' === typeof month || null === month) {
        month = date.getMonth();
    }

    do {
        result = new Date(year, month, offset);
        
        offset--;
    } while (0 === result.getDay() || 6 === result.getDay());

    return result;
}
var options = {  year: 'numeric', month: 'numeric', day: 'numeric' };

alert(event.toLocaleDateString('nl-NL', options));
Ergo: in fact the return should be the content of that alert ;)

My programming skills are very lousy, so can you adjust this script so it spits out dd-mm-yyyy?
Oleg
Site Admin
Posts: 3000
Joined: Thu Jan 01, 1970 1:00 am
Contact:

Re: Last workday of month as IF/ELSE

Post by Oleg »

However, the output is like this (and is correct by the way) (more here)

Code: Select all

Tue May 31 00:00:00 UTC+0200 2018
look at my corrections

Code: Select all

;*****************************
;* RoboTask Task file
;* Do not edit in text editor!
;*****************************
 
[Root]
ActionAfterRun=INTEGER|0
Actions=FOLDER
Automat=INTEGER|-1
CatID=INTEGER|1360203151
Comment=STRINGLIST
ContinueOnError=INTEGER|0
DoNotStopWhenShutdown=INTEGER|0
ExternalName=STRING|"Task22"
Hide=INTEGER|0
ID=INTEGER|-537864614
LogOnAsUser=INTEGER|1
Name=STRING|"Last business day of the month"
OnErrorTaskID=INTEGER|-1
Priority=INTEGER|3
RunOnClose=INTEGER|0
RunOnStartup=INTEGER|0
ToLog=INTEGER|3
UnicodeFormat=INTEGER|1
WriteGeneralLog=INTEGER|0

[Actions]
Action1=FOLDER
Action2=FOLDER

[Actions\Action1]
ActionID=STRING|"A_SCRIPT_JSEVALUATE"
Enabled=INTEGER|-1
Name=STRING|"JS Evaluate"
Params=FOLDER

[Actions\Action1\Params]
expression=STRING|"lastBusinessDayOfMonth(2018, 3)"
line00000000=STRING|"function lastBusinessDayOfMonth(year, month) {"
line00000001=STRING|"    var date = new Date();"
line00000002=STRING|"    var offset = 0;"
line00000003=STRING|"    var result = null;"
line00000005=STRING|"    if ('undefined' === typeof year || null === year) {"
line00000006=STRING|"        year = date.getFullYear();"
line00000007=STRING|"    }"
line00000009=STRING|"    if ('undefined' === typeof month || null === month) {"
line0000000A=STRING|"        month = date.getMonth();"
line0000000B=STRING|"    }"
line0000000D=STRING|"    do {"
line0000000E=STRING|"        result = new Date(year, month, offset);"
line00000010=STRING|"        offset--;"
line00000011=STRING|"    } while (0 === result.getDay() || 6 === result.getDay());"
line00000013=STRING|"    return result.getVarDate();"
line00000014=STRING|"}"
line00000016=STRING|"//LogMessage(lastBusinessDayOfMonth(2018, 4))"
linecount=STRING|"23"
loadfromfile=STRING|"1"
variable=STRING|"dt"

[Actions\Action2]
ActionID=STRING|"A_DIALOG_MESSAGE"
Enabled=INTEGER|-1
Name=STRING|"Show ""{DateTimeToFormat({dt},dd-mm-yyyy)}"""
Params=FOLDER

[Actions\Action2\Params]
icon=STRING|"1"
msg0=STRING|"{DateTimeToFormat({dt},dd-mm-yyyy)}"
msgcount=STRING|"1"
playsound=STRING|"0"
showmessage=STRING|"1"
Use the expression return result.getVarDate(); at last line of the script. In this case the script returns the string with standard system date format.
Next use the expression {DateTimeToFormat({dt},dd-mm-yyyy)} to convert standard date to necessary format.

I have changed only last line in the script

Code: Select all

function lastBusinessDayOfMonth(year, month) {
    var date = new Date();
    var offset = 0;
    var result = null;

    if ('undefined' === typeof year || null === year) {
        year = date.getFullYear();
    }

    if ('undefined' === typeof month || null === month) {
        month = date.getMonth();
    }

    do {
        result = new Date(year, month, offset);

        offset--;
    } while (0 === result.getDay() || 6 === result.getDay());

    return result.getVarDate();
}

//LogMessage(lastBusinessDayOfMonth(2018, 4))
Oleg Yershov
Rukbunker
Posts: 192
Joined: Mon Feb 22, 2016 4:06 pm
Location: Netherlands

Re: Last workday of month as IF/ELSE

Post by Rukbunker »

Many many many thanks for your reply. It works perfectly. :D
Post Reply