Skip to content

Sleeping time calculation #1027

Answered by welpdx
pirilampr asked this question in Help
Feb 9, 2023 · 1 comments · 2 replies
Discussion options

You must be logged in to vote

You got the basic gist right! Since Obsidian uses moment, we can use that with templater.

<%*  
var gt_sleep = moment(app.workspace.getActiveFile().stat.ctime);  
var w_up = moment();  

// below from stackoverflow: https://stackoverflow.com/a/29746202
// calculate total duration
var duration = moment.duration(w_up.diff(gt_sleep));
// duration in hours
var hours = parseInt(duration.asHours());
// duration in minutes
var minutes = parseInt(duration.asMinutes()) % 60;

var output = 'You slept: ' + hours + ' hour and ' + minutes + ' minutes.'
console.log(output);
tR += `${output}`  
-%>
Demo

Replies: 1 comment 2 replies

Comment options

You must be logged in to vote
2 replies
@pirilampr
Comment options

@welpdx
Comment options

Answer selected by pirilampr
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Help
Labels
None yet
2 participants