Untitled

mail@pastecode.io avatar
unknown
plain_text
2 months ago
1.7 kB
2
Indexable
Never
```dataviewjs
function newDiff(date1, date2) {
  let years = date1.diff(date2, 'years');
  date2.add(years, 'years');
  let months = date1.diff(date2, 'months');
  date2.add(months, 'months');
  let days = date1.diff(date2, 'days');
  date2.add(days, 'days');
  let hours = date1.diff(date2, 'hours');
  date2.add(hours, 'hours');
  let minutes = date1.diff(date2, 'minutes');
  let resultArray = [];
  if (years) resultArray.push(`${years}y`);
  if (months) resultArray.push(`${months}m`);
  if (days) resultArray.push(`${days}d`);
  if (hours) resultArray.push(`${hours}h`);
  if (minutes) resultArray.push(`${minutes}min`);
  const resultString = resultArray.join(' ');
  return resultString;
};

function newBar(start, end, now){
  const value = Number(((now-start)*100)/(end-start)).toFixed("2");
  const remTime = newDiff(moment(end), moment(now));
  return "<progress value='" + value + "' max='100'></progress> | " + value + " % | " + remTime;
}

const now = dv.date("now");
const nowMillis = now.toMillis();

dv.span(`
| | Progress | Percentage | Remaining |
| --- | --- |:---:|:---:|
| **Day** | ${newBar(dv.date("today").toMillis(), dv.date("tomorrow").toMillis(), nowMillis)} |
| **Week** | ${newBar(dv.date("sow").toMillis(), dv.date("eow").toMillis(), nowMillis)} |
| **Month** | ${newBar(dv.date("som").toMillis(), dv.date("eom").toMillis(), nowMillis)} |
| **Quarter** | ${newBar(DateTime.fromFormat(now.toFormat("yyyy-qq"), "yyyy-qq").toMillis(), DateTime.fromFormat(now.plus({quarter: 1}).toFormat("yyyy-qq"), "yyyy-qq").toMillis(), nowMillis)} |
| **Year** | ${newBar(dv.date("soy").toMillis(), dv.date("eoy").toMillis(), nowMillis)} |
`);
```
Leave a Comment