-
Notifications
You must be signed in to change notification settings - Fork 152
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add more examples of with() to the cookbook
Use cases from #561 suggested by @justingrant Closes: #561 See also: #240
- Loading branch information
Showing
7 changed files
with
110 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
const date = Temporal.Date.from('2020-04-14'); | ||
|
||
// Third day of next month: | ||
|
||
const thirdOfNextMonth = date.plus({ months: 1 }).with({ day: 3 }); | ||
|
||
assert.equal(thirdOfNextMonth.toString(), '2020-05-03'); | ||
|
||
// Last day of this month: | ||
|
||
const lastOfThisMonth = date.with({ day: 31 }); | ||
// Note that { disambigution: 'constrain' } is the default, so this | ||
// works even for months with fewer than 31 days. | ||
|
||
assert.equal(lastOfThisMonth.toString(), '2020-04-30'); | ||
|
||
// On the 18th of this month at 8 PM: | ||
|
||
const thisMonth18thAt8PM = date.with({ day: 18 }).withTime(Temporal.Time.from('20:00')); | ||
|
||
assert.equal(thisMonth18thAt8PM.toString(), '2020-04-18T20:00'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
const date = Temporal.Date.from('2020-05-31'); | ||
|
||
// Same date and time, but in February | ||
// (and use the last day if the date doesn't exist in February): | ||
|
||
const feb = date.with({ month: 2 }); | ||
|
||
assert.equal(feb.toString(), '2020-02-29'); | ||
|
||
// Same date and time, but in April | ||
// (and throw an exception if the date doesn't exist in April): | ||
|
||
assert.throws(() => { | ||
date.with({ month: 4 }, { disambiguation: 'reject' }); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
const birthday = Temporal.MonthDay.from('12-15'); | ||
|
||
const birthdayIn2030 = birthday.withYear(2030); | ||
birthdayIn2030.dayOfWeek; // => 7 | ||
|
||
assert(birthdayIn2030 instanceof Temporal.Date); | ||
assert.equal(birthdayIn2030.toString(), '2030-12-15'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
const date = Temporal.Date.from('2020-05-14'); | ||
|
||
const noonOnDate = date.withTime(Temporal.Time.from({ hour: 12 })); | ||
|
||
assert(noonOnDate instanceof Temporal.DateTime); | ||
assert.equal(noonOnDate.toString(), '2020-05-14T12:00'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
const time = Temporal.Time.from('12:38:28.138818731'); | ||
|
||
// explicitly: | ||
let wholeHour = time.with({ minute: 0, second: 0, millisecond: 0, microsecond: 0, nanosecond: 0 }); | ||
assert.equal(wholeHour.toString(), '12:00'); | ||
|
||
// or, taking advantage of 0 being the default for time fields: | ||
wholeHour = Temporal.Time.from({ hour: time.hour }); | ||
assert.equal(wholeHour.toString(), '12:00'); |