Skip to content

Instantly share code, notes, and snippets.

@ruqqq
Last active June 20, 2021 23:22
Show Gist options
  • Save ruqqq/44138f0f43a2873571e4f3aaa22f74b6 to your computer and use it in GitHub Desktop.
Save ruqqq/44138f0f43a2873571e4f3aaa22f74b6 to your computer and use it in GitHub Desktop.
Obsidian templater user script to get today's prayer times in markdown table format
/*
* Install: Put this script in your templater scripts folder
* Usage: (in your Obsidian template, for SG/1)
* <% tp.user.today_prayertimes("SG/1") %>
*/
const baseUrl = "https://ruqqq.github.io/prayertimes-database/data/";
async function getRequest(url) {
let response = await fetch(url);
if (!response.ok) {
throw new Error("Error performing GET request");
}
return response;
}
async function getTodayPrayerTimes(code) {
let today = new Date();
let year = today.getFullYear();
let month = today.getMonth() + 1;
let date = today.getDate();
let response = await getRequest(`${baseUrl}/${code}/${year}/${month}.json`);
let json = await response.json();
return json
.filter(item => item.date === date)[0]
.times
.map(dateStr => new Date(dateStr));
}
const names = [
"Subuh ",
"Syuruk ",
"Zuhur ",
"Asar ",
"Maghrib",
"Isyak "
];
async function main(code) {
let times = await getTodayPrayerTimes(code);
return `### Prayer Times for ${times[0].toLocaleDateString("en-GB")}\n`
+ "| Name | Time |\n"
+ "|---------|--------|\n"
+ times
.map((t, index) => {
let h = t.getHours();
let mins = t.getMinutes();
if (mins < 10) {
mins = `0${mins}`;
}
if (h > 12) {
return `| ${names[index]} | ${h - 12}:${mins}pm |`;
}
return `| ${names[index]} | ${h}:${mins}am |`;
})
.join("\n");
}
module.exports = main;
@ruqqq
Copy link
Author

ruqqq commented Jun 20, 2021

Sample output:

image
image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment