Showing posts with label date. Show all posts
Showing posts with label date. Show all posts

Wednesday, October 16, 2019

Google App Scripts - Holiday Date Checker

This script help to check whether your supplied date is holiday or not. The script rely on Google Public Holiday Calendar to determine whether the date is holiday or not.

You can open Script Editor under Tools Menu


Once you in Script editor page, you can paste the following code in the editor. Alternatively, you can also go to this link to get the latest code.


Before you can run this script, you need to add in the Google Public Holiday Calendar. For this example, i am using Singapore Public Holiday Calendar but you should be able to use this script for different country calendar.

Go to calendar page, under Other Calendars, select Browse calendars of interest.


Expand Regional holidays and tick  Holidays in Singapore. As you can see, Google has extensive list of holiday calendar for a lot of countries.



Once the calendar is setup, you can try out the script. If you are not familiar with Google App Scripts, running the script for the very first time will usually has permission prompt. Just follow the instruction and allow this script to access.





To test the script, you can just edit the following line to supply the date you want to test. The format is 'yyyy-mm-dd'.

var checkDate = new Date('2021-01-01');


Under Tools Menu, click Run - Run Function - myFunction

Once the script is done, under Tools menu, you can cick View - Logs to check the result.


You can see some sample log output below





Friday, June 14, 2013

Calculate time difference using bash script

This can be done easily if you convert your time string into epoch which is the number of seconds since 1st January 1970. Before you can do it, make sure the time string you want to convert is in correct format. One of the accepted format is "yyyy/mm/dd hh:mm:ss". In my case, the time string format is actually "yy/mm/dd/hh:mm:ss", but with little tweak by appending 20 to my time string, i can get the accepted format.

So to do that you need to use date command with -d option (display time described by STRING) and +%s format control (epoch).

Example:
currDate="2013/05/30 18:18:20"
prevDate="2013/05/30 18:18:10"
currDateEpoch=`date -d "$currDate" +%s`
prevDateEpoch=`date -d "$prevDate" +%s`
delta=$(($currDateEpoch-$prevDateEpoch))

And the delta result is 10 seconds.