dvargas92495 / SmartBlocks

Useful examples from developer community for Roam42 SmartBlocks
147 stars 7 forks source link

Get Week Number #34

Open Calhistorian opened 3 years ago

Calhistorian commented 3 years ago

📋 Describe the new feature/idea/enhancement you are requesting

Would be great to add to previous date commands to get the week number.

🔑 Why do you want this?

Being able to track weekly items by their consecutive week number is useful in any number of situations.

⚠️ Is your feature request related to a problem? Please describe.

No.

🔨 Describe any alternatives you've considered

💻 Environment

Platform: macOS

Browser: Chrome

📷 Additional Info (Screenshots, GIFs, etc.)

shabegom commented 3 years ago

@Calhistorian here's a pretty basic implementation. It will ask for a month/day and defaults to the current date if input is left blank. Could probably be adapted to grab the date from the page title if in a daily note. Hasn't been tested for every possible edge case, but generally seems to work.

credit for getWeek function: https://stackoverflow.com/questions/6117814/get-week-of-year-in-javascript-like-in-php

lswank commented 3 years ago

Slightly different example

- #42SmartBlock Get ISO Week Number Date
    - <%JAVASCRIPT:```javascript
Date.prototype.getWeekNumber = function(){
  var d = new Date(Date.UTC(this.getFullYear(), this.getMonth(), this.getDate()));
  var dayNum = d.getUTCDay() || 7;
  d.setUTCDate(d.getUTCDate() + 4 - dayNum);
  var yearStart = new Date(Date.UTC(d.getUTCFullYear(),0,1));
  return Math.ceil((((d - yearStart) / 86400000) + 1)/7)
};

let date = roam42.smartBlocks.activeWorkflow.vars.date 
let weekNumber = new Date(date ? date : new Date()).getWeekNumber()
let year = new Date(date ? date : new Date()).getFullYear()
return "[[" + year + "-W" + weekNumber + "]]"```%>
matrhein commented 3 years ago

Humm, I could be wrong but this is actually implemented now, using ww as format

Example: <%DATE:This week,ww%>