Published
Edited
Aug 31, 2022
Importers
1 star
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
startDateA = dayjs("2021-11-21")
Insert cell
We can see this date by using the toDate argument on it.
Insert cell
startDateA.toDate()
Insert cell
Insert cell
endDateA = dayjs("2050-11-21")
Insert cell
Insert cell
endDateA.toDate()
Insert cell
Insert cell
diffms = endDateA.diff(startDateA)
Insert cell
Insert cell
dayjs.duration(diffms).$d
Insert cell
Insert cell
Insert cell
Insert cell
endDateA.diff(startDateA, "years")
Insert cell
Insert cell
endDateA.diff(startDateA, "months")
Insert cell
Insert cell
Insert cell
startDateB = dayjs("2021-11-21")
Insert cell
endDateB = dayjs("2022-12-25")
Insert cell
years = endDateB.diff(startDateB, "years")
Insert cell
yearsRemainder = endDateB.subtract(years, "year")
Insert cell
months = yearsRemainder.diff(startDateB, "months")
Insert cell
monthsRemainder = yearsRemainder.subtract(months, "month")
Insert cell
days = monthsRemainder.diff(startDateB, "days")
Insert cell
daysRemainder = monthsRemainder.subtract(days, "day")
Insert cell
Insert cell
dayjs.duration({ years, months, days }).$d
Insert cell
Insert cell
viewof includeWeeks = Inputs.toggle()
Insert cell
units = ["y", "M"].concat((includeWeeks ? ["w"] : []).concat(["d", "h", "m", "s"]))
Insert cell
Insert cell
practiceDuration = dayjs.duration()
Insert cell
practiceDuration.$d
Insert cell
Insert cell
practiceDuration.add(2, "y").add(2, "d").$d
Insert cell
Insert cell
function diffDates (startDate, endDate, includeWeeks) {
const units = ["y", "M"].concat((includeWeeks ? ["w"] : []).concat(["d", "h", "m", "s"]))
const whittleDate = (givenDate, givenUnits, duration) => {
let diff = givenDate.diff(startDate, givenUnits[0])
let diffRemainder = givenDate.subtract(diff, givenUnits[0])
let newduration = duration.add(diff, givenUnits[0])
return givenUnits.length > 1 ? whittleDate(diffRemainder, givenUnits.slice(1), newduration) : newduration
}
return whittleDate(endDate, units, dayjs.duration())
}
Insert cell
Insert cell
Insert cell
testA = diffDates(startDateA, endDateA, false).$d
Insert cell
testB = diffDates(startDateB, endDateB, false).$d
Insert cell
testBreverse = diffDates(endDateB, startDateB, false).$d
Insert cell
testAwithWeeks = diffDates(startDateA, endDateA, true).$d
Insert cell
testBwithWeeks = diffDates(startDateB, endDateB, true).$d
Insert cell
diffDates(startDateA, endDateA).as("y")
Insert cell
Insert cell
Insert cell
startDateA.add(diffDates(startDateA, endDateA)).toDate()
Insert cell
startDateA.add(29, "y")
Insert cell
addDatesDiff(startDateA, diffDates(startDateA, endDateA))
Insert cell
addDatesDiff(endDateA, diffDates(endDateA, startDateA))
Insert cell
function addDatesDiff (startDate, diffDuration) {
const units = ["y", "M", "w", "d", "h", "m", "s"]
const augmentDate = (givenDate, givenDuration, givenUnits) => {
let augmentedDate = givenDate.add(givenDuration.as(givenUnits[0]), givenUnits[0])
let whittledDuration = givenDuration.subtract(givenDuration.as(givenUnits[0]), givenUnits[0])
return givenUnits.length > 1 ? augmentDate(augmentedDate, whittledDuration, givenUnits.slice(1)) : augmentedDate
}
return augmentDate(startDate, diffDuration, units)
}
Insert cell
Insert cell
dayjs = {
const dayjs = await require('dayjs')
await require('dayjs/locale/en-gb')
var relativeTime = await require('dayjs/plugin/relativeTime')
var duration = await require('dayjs/plugin/duration')
dayjs.locale('en-gb')
dayjs.extend(relativeTime)
dayjs.extend(duration)
return dayjs
}
Insert cell

One platform to build and deploy the best data apps

Experiment and prototype by building visualizations in live JavaScript notebooks. Collaborate with your team and decide which concepts to build out.
Use Observable Framework to build data apps locally. Use data loaders to build in any language or library, including Python, SQL, and R.
Seamlessly deploy to Observable. Test before you ship, use automatic deploy-on-commit, and ensure your projects are always up-to-date.
Learn more