Code Monkey home page Code Monkey logo

Comments (1)

JameeKim avatar JameeKim commented on June 26, 2024

I encountered the same problem today. I was wondering if I should try to go to bed before midnight to make tracking easier. ๐Ÿ˜† /j

TL;DR You could do one of:

  1. write your sleep times as something like -00:30 if before midnight, or
  2. create an auto-updated property field using another plugin which converts your sleep time into the above format.

What I realized was that using values like -01:30 made the chart render the way I wanted. If you're okay with using that notation in your notes, then it could be the way to go.

What I actually ended up doing involved another plugin called Metadata Menu. Using it, I set up an auto-updated property field in the frontmatter which converted my inputs into a format like [-00:30, 07:30] with the values representing the time differences from the midnight of the day for that note. I had to do this because I've been recording my times in the full datetime format. In case it helps anyone, below is the code snippet I used for the property's formula.

// My input fields are something like:
// [go-to-bed:: 2024-01-30T00:30]
// [wake-up:: 2024-01-30T07:30]
(() => {
const goToBed = current["go-to-bed"];
const wakeUp = current["wake-up"];
if (!goToBed || !wakeUp) return "";
const midnight = current.file.day;
const sleepTime = goToBed < midnight
  ? midnight.diff(goToBed).toFormat("-hh:mm")
  : goToBed.diff(midnight).toFormat("hh:mm");
const wakeUpTime = wakeUp.diff(midnight).toFormat("hh:mm");
return `[${sleepTime}, ${wakeUpTime}]`;
})()

If you're just recording times in formats like 23:00, then you just need to convert the sleep time and use the original field for the wake-up time. If you're using Metadata Menu, the formula would be something like below. (Disclaimer: I haven't tested the code)

// Let's say you have the field as:
// sleep:: 23:30/07:30
(() => {
const sleep = current.sleep?.split("/");
if (!sleep?.length) return "";
const sleepTime = dv.luxon.Duration.fromISOTime(sleep[0]);
// No change if before 12:00 (adjust as needed)
return sleepTime.hours < 12 ? sleep[0] : dv.luxon.Duration.fromObject({ hours: 24 }).minus(sleepTime).toFormat("-hh:mm");
})()

Then, you could use the new field in the tracker code block.

IIRC, a plugin called MetaEdit also allows you to create an auto-updated property field. I haven't used it yet, so I'm not sure how to get that working for this.

I hope this helps anyone reading this!

from obsidian-tracker.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.