0.10.0 • Published 3 years ago

vue-luxon v0.10.0

Weekly downloads
1,238
License
MIT
Repository
github
Last release
3 years ago

vue-luxon

DateTime formatting & parsing in Vue using Luxon

npm version npm downloads GitHub last commit GitHub version

https://packages.cblm.nl/vue-luxon

Install

npm install vue-luxon

Setup

import VueLuxon from "vue-luxon";
Vue.use(VueLuxon);

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

Change the default settings:

Vue.use(VueLuxon, {
    input: {
        zone: "utc",
        format: "iso"
    },
    output: "short"
});

Learn more about settings

Basic usage

You can use the $luxon method everywhere in your vue app:

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

Or use the luxon filter, as shown below:

{{ "2020-10-05T14:48:00.000Z" | luxon }}
// October 5, 2020

You can change the output format:

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

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

And other settings:

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

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

Settings

propoptions (default)description
inputsee settings.inputThe default input format and zone
outputsee settings.outputThe default output format, zone, language, and relative settings
templatessee templatesDefine objects to use as properties

Default settings

You can change the default settings with the second argument of the Vue.use function.

Vue.use(VueLuxon, {
  templates: {},
    input: {
        zone: "utc",
        format: "iso"
    },
    output: {
        zone: "local",
        format: {
            year: "numeric",
            month: "long",
            day: "numeric"
        },
        locale: null,
        relative: {
            round: true,
            unit: null
        }
    }
});

You can also override the default settings per method/filter easily:

{{ datetimeString | luxon({ settings }) }}
this.$luxon({ settings })

Zone

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

For the systems local zone you use local.

There is a list on wikipedia

Formats

Input and Output formats

These formats can be used as input.format and output.format

formatdescriptionin- or outputexample
sqlSQL dates, times, and datetimesboth2017-05-15 09:24:15
isoISO 8601 date time stringboth2018-01-06T13:07:04.054
rfc2822RFC 2822bothTue, 01 Nov 2016 13:23:12 +0630
httpHTTP header specs (RFC 850 and 1123)bothSun, 06 Nov 1994 08:49:37 GMT
secondsUnix timestampboth1542674993
millisUnix timestamp millisecondsboth1542674993410
tokenssee: tokensbothyyyy-MM-dd
templateNamesee: Templatesboth

Output formats

These formats can only be used as output.format

formatexample (with locale en_US)
relativesee: Relative
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

output.format can also be an object:

// using an object:
format: {
    year: 'numeric',
    month: 'long',
    day: 'numeric'
}
PropertyPossible valuesDescription
weekdaynarrow short longThe representation of the weekday
eranarrow short longThe representation of the era
yearnumeric 2-digitThe representation of the year
monthnumeric 2-digit narrow short longThe representation of the month
daynumeric 2-digitThe representation of the day
hournumeric 2-digitThe representation of the hour
minutenumeric 2-digitThe representation of the minute
secondnumeric 2-digitThe representation of the second
timeZoneNameshort longThe representation of the time zone name

settings.input

object or string

An objectcontaining a zone and format or a string of a template name.

{
	zone: "utc",
	format: "iso"
}

settings.output

object or string

An objectcontaining a zone and format or a string of a template name.

{
    zone: "local",
    format: "short",
    locale: null,
    relative: {} // see settings.relative
}

locale set to null will use the client's locale.

relative Read about the relative format below

settings.output.locale

string

null default value, this will use the client's locale.

Or use a locale tag to set a client location.

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

settings.output.relative

Set the output.format to relative to use the relative format. Or use the luxonRelative filter.

{{ datetime | luxonRelative }}
this.$luxon({ output: { format: "relative" } })

{{ datetime | luxonRelative({ style: "short" }) }}
this.$luxon({ output: { format: "relative", relative: { style: "short" } } })

You can change the behavior with the relative settings object in theoutput` .

{
    output: {
        format: "relative"
        relative: {
            style: "long",
            unit: null,
            round: true,
            padding: 0
        },
    }
}
propertydescriptiondefault
stylethe style of units, must be "long", "short", or "narrow"long
unituse a specific unit; if omitted, the method will pick the unit. Use one of "years", "quarters", "months", "weeks", "days", "hours", "minutes", or "seconds"null
roundwhether to round the numbers in the output.true
paddingpadding in milliseconds. This allows you to round up the result if it fits inside the threshold. Don't use in combination with {round: false} because the decimal output will include the padding.0

Templates

You can predefine setting templates.

By default there is a server, client and a inputdate template, but you can add your own to the options object.

It's also possible to use a template in a template, as the inputdate uses the client template's zone for example.

templates: {
    server: {
        zone: "utc",
        format: "iso"
    },
    client: {
        zone: "local",
        format: "short"
    },
    inputdate: {
        zone: "client",
        format: "yyyy-MM-dd"
    }
}

There are multiple ways to use a template:

// This will use the templates zone and format
{{ "2020-10-05T14:48:00.000Z" | luxon({ input: "server" }) }}

// This will use the templates zone
{{ "2020-10-05T14:48:00.000Z" | luxon({ input: { zone: "client" } }) }}

Or you can set the default input and output in the Vue.use function to use these templates by default:

Vue.use(VueLuxon, {
    input: "server",
    output: "client",
});

Or create custom templates to use everywhere:

Vue.use(VueLuxon, {
	templates: {
		serverAMS: {
			zone: "Europe/Amsterdam",
			format: "dd-MM-yyyy HH:mm:ss"
		},
        serverUTC: {
			zone: "UTC",
			format: "yyyy-MM-dd HH:mm:ss"
		},
        clientAMS: {
            zone: "Europe/Amsterdam",
			format: "med"
        }
	},
    input: "serverUTC",
    output: "clientAMS",
});

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

Change the $luxon method name

Provide a methodName in the settings object.


Tips

  • Save and serve your datetimes from the server in the utc time zone and the iso or sql format. Then use the client's locale format.

Changelog

0.10.0

  • output.lang is changed to output.locale and the locale is now always set.
  • ESM version added

0.9.0

  • New API
0.10.0

3 years ago

0.9.0

3 years ago

0.8.1

4 years ago

0.8.0

4 years ago

0.7.4

4 years ago

0.7.3

4 years ago

0.7.2

4 years ago

0.7.1

4 years ago

0.7.0

5 years ago

0.6.2

6 years ago

0.6.1

6 years ago

0.6.0

6 years ago

0.5.5

6 years ago

0.5.4

6 years ago

0.5.3

6 years ago

0.5.1

6 years ago

0.5.0

6 years ago

0.3.1

6 years ago

0.3.0

6 years ago

0.2.7

6 years ago

0.2.6

6 years ago

0.2.5

6 years ago

0.2.4

6 years ago

0.2.3

6 years ago

0.2.2

6 years ago

0.2.0

6 years ago

0.1.4

6 years ago

0.1.3

6 years ago

0.1.2

6 years ago

0.1.1

6 years ago

0.1.0

6 years ago

0.0.1

6 years ago