Module

Data.Intl.DateTimeFormat

Package
purescript-intl
Repository
coot/purescript-intl

#LocalesOption Source

type LocalesOption = Variant (locale :: String, locales :: Array String, undefined :: Undefined)

#LocaleMatcher Source

#TimeZone Source

#FormatMatcher Source

#DateTimeComponents Source

type DateTimeComponents a = Variant (custom :: a, hourMinute :: HourMinute, hourMinuteSecond :: HourMinuteSecond, monthDay :: MonthDay, weekdayYearMonthDay :: WeekdayYearMonthDay, weekdayYearMonthDayHourMinuteSecond :: WeekdayYearMonthDayHourMinuteSecond, yearMonth :: YearMonth, yearMonthDay :: YearMonthDay)

#DateTimeFormatOptions Source

data DateTimeFormatOptions a

Constructors

#DateTimeFormat Source

#createDateTimeFormat Source

createDateTimeFormat :: forall a. FormatComponent a => LocalesOption -> DateTimeFormatOptions a -> F DateTimeFormat

Examples

import Data.Variant (inj)

fmtHourMinute = F DateTimeFormat
fmtHourMinute = createDateTimeFormat locale opts
   where
     locale = inj (SProxy :: SProxy "locale") "en-GB"
     opts = (DateTimeFormatOptions
               { localeMatcher: Nothing
               , timeZone: (Just (TimeZone "Europe/London"))
               , hour12: Just false
               , formatMatcher: Nothing
               }
               (inj (SProxy :: SProxy "hourMinute")
                    (HourMinute {hour: TwoDigit, minute: TwoDigit}))


fmtDate :: F DateTimeFormat
fmtDate = createDateTimeFormat locale opts
  wher
    locale = inj (SProxy :: SProxy "locale") "en-GB"
    opts :: DateTimeFormatOptions'
    opts =
      (DateTimeFormatOptions
        { localeMatcher: Nothing
        , timeZone: (Just (TimeZone "Europe/London"))
        , hour12: Just true
        , formatMatcher: Nothing
        }
        (inj
          (SProxy :: SProxy "weekdayYearMonthDayHourMinuteSecond")
          (WeekdayYearMonthDayHourMinuteSecond
            { weekday: Short
            , year: Numeric
            , month: MonthLong
            , day: Numeric
            , hour: Numeric
            , minute: Numeric
            , second: Numeric
            }
          )
        )
      )

You have predifined types: WeekdayYearMonthDayHourMinuteSecond, WeekdayYearMonthDay, YearMonthDay, YearMonth, MonthDay, HourMinuteSecond, HourMinute. But you can also provide your own by implementing FormatComponent class (or by deriving it), e.g.

newtype EraYear = EraYear
  { era :: StringRep
  , year :: NumericRep
  }

derive instance genericEraYear :: Generic EraYear _

instance formatComponentEraYear :: FormatComponent EraYear where
  formatComponent = genericFormatComponent

fmtEraYear :: F DateTimeFormat
fmtEraYear = createDateTimeFormat locales opts
  where
    locale = inj (SProxy :: SProxy "locale") "en-GB"
    opts :: DateTimeFormatOptions EraYear
    opts =
      (DateTimeFormatOptions
        { localeMatcher: Nothing
        , timeZone: (Just (TimeZone "Europe/London"))
        , hour12: Just true
        , formatMatcher: Nothing
        }
        (inj
          (SProxy :: SProxy "custom")
          (EraYear
            { era: Long
            , year: Numeric
            }
          )
        )
      )

#formatJSDate Source

formatJSDate :: DateTimeFormat -> JSDate -> String

Examples

formatJSDate (either (const "") id $ runExcept fmtHourMinute) date -- "12:00"
formatJSDate (either (const "") id $ fmtDate) date                 -- "Tue, January 2, 2018, 12:00:00 PM"
formatJSDate (either (const "") id $ fmtEraYear) date              -- "2018 Anno Domini"

Re-exports from Data.Intl.DateTimeFormat.Types

#YearMonthDay Source

#YearMonth Source

newtype YearMonth

Constructors

Instances

#Undefined Source

data Undefined :: Type

#StringRep Source

#ResolvedOptions Source

newtype ResolvedOptions

Constructors

Instances

#NumericRep Source

#MonthDay Source

newtype MonthDay

Constructors

Instances

#HourMinute Source