Skip to content

Instantly share code, notes, and snippets.

@Oluwasetemi
Last active May 4, 2024 02:47
Show Gist options
  • Save Oluwasetemi/8ddea23dea82b5373a729094b40dfea9 to your computer and use it in GitHub Desktop.
Save Oluwasetemi/8ddea23dea82b5373a729094b40dfea9 to your computer and use it in GitHub Desktop.
import 'temporal-polyfill/global'
const now = Temporal.Now.instant().toString()
const currentTimeZoneId = Temporal.Now.timeZoneId()
console.log(Temporal.Now.zonedDateTimeISO().toString())
const currentTimeZone= Temporal.Now.zonedDateTimeISO().toString()
// methods - zonedDateTimeISO, zoneDateTime, instant, timeZoneId, plainDateTimeISO, plainDateISO, plainTimeISO, plainDateTime, plainDate.
let financialCentres = {
'New York': 'America/New_York',
London: 'Europe/London',
Tokyo: 'Asia/Tokyo'
};
console.log(`Here: ${Temporal.Now.zonedDateTimeISO()}`);
Object.entries(financialCentres).forEach(([name, timeZone]) => {
console.log(`${name}: ${Temporal.Now.zonedDateTimeISO(timeZone)}`);
});
// plainDateTimeISO
const plainDateTimeISO = Temporal.Now.plainDateTimeISO().toString()
// plainDate
const plainDate = Temporal.Now.plainDateISO().toString()
// plainTime
const plainTime = Temporal.Now.plainTimeISO().toString()
// Temporal.ZoneDateTime = TimeZone + Instant + PlainDateTime + Calendar
let birthday = Temporal.ZonedDateTime.from({
timeZone: 'Universal',
year: 1993,
month: 2,
day: 12,
})
console.log(birthday.toString())
// Constructor (new Temporal.ZonedDateTime(epochNanoseconds: bigint, timeZone: string | object, calendar: string | object = "iso8601"))
// Static methods (from, compare)
// Properties (year month day hour minute second millisecond microsecond nanosecond epochSeconds epochMilliseconds epochMicroseconds epochNanoseconds calendarId timeZoneId era eraYear dayOfWeek dayOfYear weekOfYear yearOfWeek daysInWeek daysInMonth daysInYear monthsInYear inLeapYear hoursInDay offsetNanoseconds)
// Methods (with withPlainTime withPlainDate withTimeZone withCalendar add subtract until since round startOfDay equals toString toLocaleString toJSON valueOf toInstant toPlainDate toPlainTime toPlainDateTime toPlainYearMonth toPlainMonthDay getCalendar getTimeZone getISOFields)
// Temporal PlainDate can be converted to Temporal.PlainYearMonth and Temporal.PlainMonthDay
let date = Temporal.PlainDate.from('2021-10-10')
date.inLeapYear
date.dayOfWeek
date.dayOfYear
date.daysInMonth
date.daysInYear
date.month
date.monthCode
date.toString()
// Constructor new Temporal.PlainDate
// Static methods from compare
// Properties year month monthCode day calendarId era eraYear dayOfWeek dayOfYear weekOfYear yearOfWeek daysInWeek daysInMonth daysInYear monthsInYear inLeapYear
// Methods with withCalendar add subtract until since equals toString toLocaleString toJSON valueOf toZonedDateTime toPlainDateTime toPlainYearMonth toPlainMonthDay getCalendar getISOFields
// Temporal.PlainTime
let time = Temporal.PlainTime.from('10:10:10')
time.toString()
// Constructor new Temporal.PlainTime
// Static methods from compare
// Properties hour minute second millisecond microsecond nanosecond
// Methods with add subtract until since round equals toString toLocaleString toJSON valueOf toZonedDateTime toPlainDateTime getISOFields
// Temporal.PlainDateTime converted to ZoneDateTime using Temporal.TimeZone
let dateTime = Temporal.PlainDateTime.from('2021-10-10T10:10:10')
dateTime.toString()
//add 5 minutes and 5 seconds to the current time
const fiveMinutesLater = dateTime.with({
minute: dateTime.minute + 5,
second: dateTime.second + 5,
})
fiveMinutesLater.toString()
// Constructor new Temporal.PlainDateTime
// Static methods from compare
// Properties year month monthCode day hour minute second millisecond microsecond nanosecond calendarId era eraYear dayOfWeek dayOfYear weekOfYear yearOfWeek daysInWeek daysInMonth daysInYear monthsInYear inLeapYear
// Methods with withPlainTime withPlainDate withCalendar add subtract until since round equals toString toLocaleString toJSON valueOf toZonedDateTime toPlainDate toPlainYearMonth toPlainMonthDay toPlainTime getCalendar getISOFields
// Temporal.PlainYearMonth
let yearMonth = Temporal.PlainYearMonth.from('2020-10')
yearMonth.daysInMonth
yearMonth.daysInYear
yearMonth.toString()
// Constructor new Temporal.PlainYearMonth
// Static methods from compare
// Properties year month monthCode calendarId era eraYear daysInMonth daysInYear monthsInYear inLeapYear
// Methods with add subtract until since equals toString toLocaleString toJSON valueOf toPlainDate getCalendar getISOFields
// Temporal.PlainMonthDay
let monthDay = Temporal.PlainMonthDay.from('12-25')
monthDay.toString()
monthDay.toPlainDate({ year: 2024}).toString()
// Constructor new Temporal.PlainMonthDay
// Static methods from
// Properties monthCode day calendarId
// Methods with equals toString toLocaleString toJSON valueOf toPlainDate getCalendar getISOFields
// Temporal.Duration
const durationObj = new Temporal.Duration(1, 2, 3, 4, 5, 6, 7, 987, 654, 321);
durationObj.toString();
const duration = Temporal.Duration.from({
hours: 5,
minutes: 20
});
duration.total({ unit: 'minutes' }).toString();
// Constructor new Temporal.Duration(years?: number, months?: number, weeks?: number, days?: number, hours?: number, minutes?: number, seconds?: number, milliseconds?: number, microseconds?: number, nanoseconds?: number)
// Static methods from compare
// Properties years months weeks days hours minutes seconds milliseconds microseconds nanoseconds sign blank
// Methods with add subtract negated abs round total toString toJSON toLocaleString valueOf
// table
// ISO 8601 Meaning
// P1Y1M1DT1H1M1.1S One year, one month, one day, one hour, one minute, one second, and 100 milliseconds
// P40D Forty days
// P1Y1D A year and a day
// P3DT4H59M Three days, four hours and 59 minutes
// PT2H30M Two and a half hours
// P1M One month
// PT1M One minute
// PT0.0021S 2.1 milliseconds (two milliseconds and 100 microseconds)
// PT0S Zero
// P0D Zero
// Temporal.TimeZone
let timeZone = Temporal.TimeZone.from('America/New_York')
timeZone.getInstantFor('2021-10-10T10:10:10').toString()
timeZone.getPlainDateTimeFor('2000-01-01T00:00Z').toString(); // => 2000-01-01T02:00:00
timeZone.getPreviousTransition(Temporal.Now.instant()).toString(); // => 2014-09-25T21:00:00Z
timeZone.getNextTransition(Temporal.Now.instant()).toString(); // => null
// Constructor new Temporal.TimeZone Difference between IANA time zones and numeric UTC offsets
// Static methods from
// Properties id
// Methods equals getOffsetNanosecondsFor getOffsetStringFor getPlainDateTimeFor getInstantFor getPossibleInstantsFor getNextTransition getPreviousTransition toString toJSON
// Temporal.Calendar
let calendar = Temporal.Calendar.from('islamic') // gregory, iso8601, buddhist, chinese, coptic, ethiopic, hebrew, indian, islamic, japanese, persian, roc
calendar.id
calendar.toString()
calendar.dateFromFields({ year: 1993, month: 2, day: 12 }).toString()
// Constructor new Temporal.Calendar
// Static methods from
// Properties id
// Methods era eraYear year month monthCode day dayOfWeek dayOfYear weekOfYear yearOfWeek daysInWeek daysInMonth daysInYear monthsInYear inLeapYear dateFromFields yearMonthFromFields monthDayFromFields dateAdd dateUntil fields mergeFields toString toJSON
// Temporal.Instant
let instant = Temporal.Instant.from('2021-10-10T10:10:10Z')
instant.toString()
// Constructor (new Temporal.Instant(0n))
// Static methods (from, fromEpochSeconds, fromEpochMilliseconds, fromEpochMicroseconds, fromEpochNanoseconds, compare)
// Properties (epochSeconds, epochMilliseconds, epochMicroseconds, epochNanoseconds)
// Methods (toZoneDateTimeISO, toZoneDateTime, add, subtract, until, since, round, equals, toString, toLocaleString, toJSON, valueOf)
let startOfMoonMission = Temporal.Instant.from('1969-07-16T13:32:00Z');
let endOfMoonMission = Temporal.Instant.from('1969-07-24T16:50:35Z');
let missionLength = startOfMoonMission.until(endOfMoonMission, { largestUnit: 'hour' });
missionLength.toString();
console.log(missionLength.toLocaleString());
// https://tc39.es/proposal-temporal/docs/persistence-model.svg
// Cookbook https://tc39.es/proposal-temporal/docs/cookbook.html
// Spec https://github.com/fullcalendar/temporal-polyfill/blob/main/packages/temporal-spec/index.d.ts
// https://github.com/tc39/proposal-temporal
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment