Last active
December 21, 2022 22:02
-
-
Save JacobWeisenburger/551056d658b3cc7cba4901b1e9eb666b to your computer and use it in GitHub Desktop.
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
import { Temporal } from 'temporal' | |
const TemporalDateUtils = ( datelike: string | Date ) => { | |
const date = typeof datelike === 'string' ? new Date( datelike ) : datelike | |
const datelikeString = typeof datelike === 'string' ? datelike : date.toISOString() | |
const instant = Temporal.Instant.from( date.toISOString() ) | |
const plainDateString = instant.toString().split( 'T' )[ 0 ] | |
const timeIncluded = /T\d+/.test( datelikeString ) | |
const toPlainDate = () => Temporal.PlainDate.from( plainDateString ) | |
const toPlainDateTime = () => Temporal.PlainDateTime.from( | |
timeIncluded | |
? instant.toString().replaceAll( 'Z', '' ) | |
: toPlainDate() | |
) | |
return { | |
toPlainDate, | |
toPlainDateTime, | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment