实现功能一览:

在日记中的记录:

图1:日记中的记录

在月记中的统计实现:

图2:月记中的统计实现

说明:

  • 标签列是一级标签,明细列是一级标签下的子标签,只支持两级标签。
  • 时长统计根据日记中的逐行做差得到。

实现:

所需插件:

  • QucikAdd
  • DataView

日记中的实现:

  1. 指定一个节名,如图1中的"Time Log",这对应代码中的:l.section?.subpath === "Time Log"
  2. 手动记录每次任务开始时的时间,并加上标签,记录格式为- hh:mm do what #tag
    • 每次手动输入时间是很麻烦的,可以利用QuickAdd插件制作模板,实现使用快捷键在日记中的指定位置插入当前时间以及你想记录的内容,这一小部分的教程请参考:少数派-当 Obsidian 与时间管理相遇 该教程中有两个错误:1. 创建 QuickAdd 的时候,选择 capture 而不是截图中的 template 2. capture format value 是小写。大写的VALUE 无法捕获。
    • 注意最后一行需要有个不带标签的结束,例如图1中的"END",这样才能完整做差,计算时间。
  3. 需要在日记模板中加入日期与本年第几周的属性:
图3:日记中的笔记属性
  • 属性的代码实现如下:
    ---
    date: <% tp.file.creation_date("YYYY-MM-DD") %>
    NO.week: <% window.moment().format("GGGG-[W]WW") %>
    ---

月记中的实现:

月记名为"YYYY-MM"格式,不是的话代码无法自动提取,需要你在代码中自行指定月份。

代码如下,注释挺良好的,记得改下文件夹路径之类的,其他看不懂的,GPT都懂:

/***********************
 * 工具函数
 ***********************/
function parseTime(t) {
  const [h, m] = t.split(":").map(Number);
  return h * 60 + m;
}

function getMonth(day) {
  return `${day.year}-${String(day.month).padStart(2, "0")}`;
}

/***********************
 * 当前月(从月记文件名取)
 ***********************/
const currentMonth = dv.current().file.name.match(/\d{4}-\d{2}/)?.[0];

/***********************
 * 数据结构,不用管,自动填入
 ***********************/
// daily[date][root] = { total, children }
// weekly[week][root] = { total, children }
// monthly[root] = { total, children }

let daily = {};
let weekly = {};
let monthly = {};

/***********************
 * 只有在能识别月份时才执行统计
 ***********************/
if (currentMonth) {

  /***********************
   * 扫描本月所有日记,在“日记”文件夹下
   ***********************/
  for (const page of dv.pages('"日记"')) {

    if (!page.date) continue;

    const pageDate = dv.date(page.date);
    if (getMonth(pageDate) !== currentMonth) continue;

    const dateKey = dv.date(page.date).toFormat("yyyy-MM-dd");
    const weekKey = page["NO.week"] ?? "Unknown";

    daily[dateKey] ??= {};
    weekly[weekKey] ??= {};

    const logs = page.file.lists.filter(
      l => l.section?.subpath === "Time Log" //Time Log是你自定义的节名
    );

    for (let i = 0; i < logs.length - 1; i++) {

      const curr = logs[i].text;
      const next = logs[i + 1].text;

      const t1 = curr.match(/\d{2}:\d{2}/)?.[0];
      const t2 = next.match(/\d{2}:\d{2}/)?.[0];
      if (!t1 || !t2) continue;

      const hours = (parseTime(t2) - parseTime(t1)) / 60;
      if (hours <= 0) continue;

      const tags = curr.match(/#\S+/g) ?? [];

      for (const tag of tags) {

        const parts = tag.slice(1).split("/");
        const root = "#" + parts[0];
        const child = parts.length > 1 ? parts.slice(1).join("/") : null;

        // 向 daily / weekly / monthly 三个桶同时累加
        for (const bucket of [
          daily[dateKey],
          weekly[weekKey],
          monthly
        ]) {
          bucket[root] ??= { total: 0, children: {} };
          bucket[root].total += hours;

          if (child) {
            bucket[root].children[child] =
              (bucket[root].children[child] ?? 0) + hours;
          }
        }
      }
    }
  }

  /***********************
   * 输出工具
   ***********************/
    function renderTable(title, data, firstCol, withSubtotal = false) {
      dv.header(3, title);

      dv.table(
        [firstCol, "标签", "总时长", "明细"],
        Object.entries(data)
          // 排序(weekly / monthly 都安全)
          .sort(([a], [b]) => dv.date(a) - dv.date(b))
          .flatMap(([key, roots]) => {

            const rows = [];
            let subtotal = 0;

            const entries = Object.entries(roots);

            entries.forEach(([root, info], idx) => {
              subtotal += info.total;

              const childText = Object.entries(info.children)
                .sort((a, b) => b[1] - a[1])
                .map(([k, v]) => `${k}: ${v.toFixed(1)}h`) //toFixed中的"1"是小数点后1位
                .join("<br>");

              rows.push([
                idx === 0 ? key : "",
                root,
                info.total.toFixed(1) + "h",
                childText || "<span style='color: gray;'>无</span>"
              ]);
            });

            // ⭐ 小计行(只在 weekly / monthly 开启)
            if (withSubtotal && entries.length > 0) {
              rows.push([
                "",
                "小计",
                `${subtotal.toFixed(1)}h`,
                ""
              ]);
            }

            return rows;
          })
      );
    }

  /***********************
   * 三张表
   ***********************/
  renderTable("📊 本月统计", { "本月": monthly }, "范围", true);
  renderTable("🗓️ 每周统计", weekly, "周", true);
  renderTable("⏳ 逐天分布", daily, "日期", false);

} else {
  dv.paragraph("❌ 月记文件名中未找到 YYYY-MM");
}

其他:

  • 推荐了解:插件-worklogger,实现了QuickAdd的自动记录和月记中自动统计时长的功能,界面优雅,支持接入大模型编写周报等。但对我来说,封装度有点高,我不想为了time log单独建议一个文件夹。
  • 可结合Charts 插件做一些更精细化的分析,实现可请教GPT大人。
1
1