I program fairly complex cards for AnkiDroid. If anybody is interested in what I learned along the way, I could post some stuff. Anyone interested?
1 Like
Always. Thank you!
Especially see last function.
Much of my code is Typescript, but that can easily be converted to JS.
class Statics {
// ******** Config
/** The date when the collection was created. Unconveniently, this is needed to calculate the due date from
* the value the API returns. */
private static dateOfCreationOfCollection = new Date(2023, 5, 12);
public static daysSinceCardModified() {
const unixTimestamp = window["AnkiDroidJS"].ankiGetCardMod();
const dateFromTimestamp = new Date(unixTimestamp * 1000); // convert to milliseconds
const today = new Date();
const differenceInTime = today.getTime() - dateFromTimestamp.getTime();
const differenceInDays = differenceInTime / (1000 * 3600 * 24);
return differenceInDays;
}
public static daysSinceCreationOfCollection() {
const diffTime = Math.abs(new Date().getTime() - Statics.dateOfCreationOfCollection.getTime());
return Math.ceil(diffTime / (1000 * 60 * 60 * 24));
}
public static daysSinceCardWasDue() {
let days = Statics.daysSinceCreationOfCollection() - window["AnkiDroidJS"].ankiGetCardDue();
if (0 <= days && days < 10*365)
return days;
// Strange number of days. Thus, we assume that it is a new card, for which the API function above returns a random integer.
return 0;
}
public static intervalOfCard() {
return window["AnkiDroidJS"].ankiGetCardInterval();
}
/**
* @return {string} the time since the card was last shown to the user
* or "" if the card is due today or not due yet.
*
* I have lots of overdue cards and I find it very useful to display the number of
* days since the card was last seen. */
public static timeSinceLastSeenAsString(): string {
const daysSinceCardWasDue = Statics.daysSinceCardWasDue();
if (daysSinceCardWasDue === 0) {
return "";
}
const daysSince = daysSinceCardWasDue + Statics.intervalOfCard();
if (daysSince>=75) {
return (daysSince/365.25).toFixed(1) + " y = " + daysSince + " d";
}
if (daysSince>=25) {
return (daysSince/(365.25/12)).toFixed(1) + " m = " + daysSince + " d";
}
return daysSince + " d";
}
}
1 Like
This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.