1.2.2 • Published 9 months ago

vue-lux v1.2.2

Weekly downloads
-
License
MIT
Repository
github
Last release
9 months ago

vue-lux

npm version npm downloads bundle

vue-lux is a Vue 3 library that simplifies DateTime formatting and parsing using the powerful Luxon library. Inspired by vue-luxon.

Install

yarn add vue-lux
pnpm i vue-lux
npm i vue-lux

Vue

import { createApp } from 'vue'
import VueLuxon from 'vue-lux'
import App from './App.vue'

const app = createApp(App)
app.use(VueLux, {
  input: {
    zone: 'utc',
    format: 'iso'
  },
  output: 'short'
})
app.mount('#app')

Nuxt

export default defineNuxtConfig({
  modules: ['vue-lux/nuxt'],
  lux: {
    input: {
      zone: 'utc',
      format: 'iso'
    },
    output: 'short'
  }
})

By default, vue-lux expect the given datetime string to be time zone utc and format iso . The output will be based on the client's locale.

Learn more about settings

Formatting

You can use the $lux or $lf method everywhere in your vue app to formate a date:

this.$lux('2020-10-05T14:48:00.000Z')
// October 5, 2020

You can change the output format:

this.$lux('2020-10-05T14:48:00.000Z', 'dd-MM-yyyy')
// 05-10-2020

this.$lux('2020-10-05 22:36', 'relative')
// 22 days ago

And other settings:

this.$lux('2020-10-05 22:36', 'full', { format: 'yyyy-MM-dd HH:mm', zone: 'Asia/Tokyo' })
// October 5, 2020, 3:36 PM GMT+2

These formats will be in the clients browser language, unless you set a specific language.

Parsing

You can use the $lp method to parse a date and retrive a Luxon DateTime object:

this.$lp('2020-10-05T14:48:00.000Z')
// DateTime { 2020-10-05T14:48:00.000Z }

this.$lp('2020-10-05 22:36', 'yyyy-MM-dd HH:mm')
// DateTime { 2020-10-05T22:36:00.000Z }

Settings

You can define the default input and output settings in the plugin options.

{
  input: {
    zone: 'utc',
    format: 'iso',
  },
  output: {
    locale: 'en',
    format: 'short',
  },
  templates: {
    userDate: {
      zone: 'client',
      format: 'dd MM yyyy',
    },
    serverAMS: {
      zone: 'Europe/Amsterdam',
      format: 'dd-MM-yyyy HH:mm:ss',
    },
    client: {
      zone: 'local',
      format: 'short',
    },
  },
}

Templates

Templates are predefined formats that can be used to quickly format and parse.

// Will use the template userDate to format the date
this.$lux('2020-10-05T14:48:00.000Z', 'userDate')
// 05 10 2020

// Will use serverAMS template to parse the date
this.$lp('2020-10-05 22:36', 'serverAMS')
// DateTime { 2020-10-05T22:36:00.000Z }

// Will use serverAMS template to parse and userDate template to format the date
this.$lux('2020-10-05 22:36', 'userDate', 'serverAMS')
// 05 10 2020

By default there are these templates available:

formatexample (with locale en_US)
short10/14/1983, 1:30 PM
shorts10/14/1983, 1:30:23 PM
medOct 14, 1983, 1:30 PM
medsOct 14, 1983, 9:30:33 AM
fullOctober 14, 1983, 9:30 AM EDT
fullsOctober 14, 1983, 9:30:33 AM EDT
hugeFriday, October 14, 1983, 9:30 AM Eastern Daylight Time
hugesFriday, October 14, 1983, 9:30:33 AM Eastern Daylight Time
time9:30 AM
times09:30:23 AM
time2409:30
time24s09:30:23
time24longoffset09:30:23 Eastern Daylight Time
date_fullOctober 14, 1983
date_hugeTuesday, October 14, 1983
date_medOct 14, 1983
date_meddFri, Oct 14, 1983
date_short10/14/1983

Input

The input can be one of the following types:

  • string: A string representation of a date or time.
  • number: A numeric representation of a date or time, such as a timestamp.
  • Date: A JavaScript Date object.
  • DateTime: A DateTime object from the Luxon library.

Along with the input, you can also specify the input format and zone.

import type { Zone } from 'luxon'

interface InputOptions {
  format: string
  zone?: string | Zone
}

Available formats are:

formatdescriptionexample
sqlSQL dates, times, and datetimes2017-05-15 09:24:15
isoISO 8601 date time string2018-01-06T13:07:04.054
rfc2822RFC 2822Tue, 01 Nov 2016 13:23:12 +0630
httpHTTP header specs (RFC 850 and 1123)Sun, 06 Nov 1994 08:49:37 GMT
secondsUnix timestamp1542674993
millisUnix timestamp milliseconds1542674993410
DateJavaScript Date objectnew Date('2020-10-05T14:48:00.000Z')
DateTimeLuxon DateTime objectDateTime.fromISO('2020-10-05T14:48:00.000Z')
tokenssee: Tokensyyyy-MM-dd
templateNamesee: Templates

Output

import type { DateTimeFormatOptions, ToISOTimeOptions, ToRelativeOptions, ToSQLOptions, Zone } from 'luxon'

interface OutputOptions {
  locale?: string
  format?: string | DateTimeFormatOptions | Intl.DateTimeFormatOptions
  relative?: ToRelativeOptions
  sql?: ToSQLOptions
  iso?: ToISOTimeOptions
  zone?: string | Zone
}
  • format: The format to use for formatting.

    • Set of Tokens: You can use a set of tokens to define the output format. Tokens represent different parts of the date and time, such as year, month, day, hour, minute, etc. See Tokens for possible values.
    • Template Name: You can specify the name of a predefined template. Templates are predefined formats that can be used to quickly format dates and times. See Templates for possible values.
    • DateTimeFormatOptions: Options for the DateTime format. See DateTimeFormatOptions for possible values.
  • locale: The locale to use for formatting. If not set, the client's locale will be used.

  • relative: Options for the relative format. See Relative for possible values.
  • sql: Options for the SQL format. See SQL for possible values.
  • iso: Options for the ISO format. See ISO for possible values.

Zone

eg: UTC, America/New_York, Asia/Tokyo, ...

For the systems local zone you use local.

There is a list on wikipedia

Locale

Examples:

en: English (primary language). hi: Hindi (primary language). de-AT: German as used in Austria (primary language with country code). zh-Hans-CN: Chinese written in simplified

The client's locale will be used if not set.

Tokens

Tokens are useful for formatting and parsing.

You can use the following tokens:

Standalone tokenFormat tokenDescriptionExample
Smillisecond, no padding54
SSSmillisecond, padded to 3054
ufractional seconds, functionally identical to SSS054
ssecond, no padding4
sssecond, padded to 2 padding04
mminute, no padding7
mmminute, padded to 207
hhour in 12-hour time, no padding1
hhhour in 12-hour time, padded to 201
Hhour in 24-hour time, no padding9
HHhour in 24-hour time, padded to 213
Znarrow offset+5
ZZshort offset+05:00
ZZZtechie offset+0500
ZZZZabbreviated named offsetEST
ZZZZZunabbreviated named offsetEastern Standard Time
zIANA zoneAmerica/New_York
ameridiemAM
dday of the month, no padding6
ddday of the month, padded to 206
cEday of the week, as number from 1-7 (Monday is 1, Sunday is 7)3
cccEEEday of the week, as an abbreviate localized stringWed
ccccEEEEday of the week, as an unabbreviated localized stringWednesday
cccccEEEEEday of the week, as a single localized letterW
LMmonth as an unpadded number8
LLMMmonth as an padded number08
LLLMMMmonth as an abbreviated localized stringAug
LLLLMMMMmonth as an unabbreviated localized stringAugust
LLLLLMMMMMmonth as a single localized letterA
yyear, unpadded2014
yytwo-digit year14
yyyyfour- to six- digit year, pads to 42014
Gabbreviated localized eraAD
GGunabbreviated localized eraAnno Domini
GGGGGone-letter localized eraA
kkISO week year, unpadded17
kkkkISO week year, padded to 42014
WISO week number, unpadded32
WWISO week number, padded to 232
oordinal (day of year), unpadded218
oooordinal (day of year), padded to 3218
Dlocalized numeric date9/4/2017
DDlocalized date with abbreviated monthAug 6, 2014
DDDlocalized date with full monthAugust 6, 2014
DDDDlocalized date with full month and weekdayWednesday, August 6, 2014
tlocalized time9:07 AM
ttlocalized time with seconds1:07:04 PM
tttlocalized time with seconds and abbreviated offset1:07:04 PM EDT
ttttlocalized time with seconds and full offset1:07:04 PM Eastern Daylight Time
Tlocalized 24-hour time13:07
TTlocalized 24-hour time with seconds13:07:04
TTTlocalized 24-hour time with seconds and abbreviated offset13:07:04 EDT
TTTTlocalized 24-hour time with seconds and full offset13:07:04 Eastern Daylight Time
fshort localized date and time8/6/2014, 1:07 PM
ffless short localized date and timeAug 6, 2014, 1:07 PM
fffverbose localized date and timeAugust 6, 2014, 1:07 PM EDT
ffffextra verbose localized date and timeWednesday, August 6, 2014, 1:07 PM Eastern Daylight Time
Fshort localized date and time with seconds8/6/2014, 1:07:04 PM
FFless short localized date and time with secondsAug 6, 2014, 1:07:04 PM
FFFverbose localized date and time with secondsAugust 6, 2014, 1:07:04 PM EDT
FFFFextra verbose localized date and time with secondsWednesday, August 6, 2014, 1:07:04 PM Eastern Daylight Time
qquarter, no padding9
qqquarter, padded to 213

Full list of tokens can be found here

License

MIT License © 2024-PRESENT Donald Shtjefni

1.2.2

9 months ago

1.2.1

9 months ago

1.1.0

9 months ago