Reflex Cloud - Fast, secure & scalable hosting. One command to deploy.

Moment

Displaying date and relative time to now sometimes can be more complicated than necessary.

To make it easy, Reflex is wrapping react-moment under rx.moment.

Examples

Using a date from a state var as a value, we will display it in a few different way using rx.moment.

The date_now state var is initialized when the site was deployed. The button below can be used to update the var to the current datetime, which will be reflected in the subsequent examples.

Display the date as-is:

Humanized interval

Sometimes we don't want to display just a raw date, but we want something more instinctive to read. That's when we can use from_now and to_now.

You can also set a duration (in milliseconds) with from_now_during where the date will display as relative, then after that, it will be displayed as defined in format.

Formatting dates

Offset Date

With the props add and substract, you can pass an rx.MomentDelta object to modify the displayed date without affecting the stored date in your state.

Timezones

You can also set dates to display in a specific timezone:

Client-side periodic update

If a date is not passed to rx.moment, it will use the client's current time.

If you want to update the date every second, you can use the interval prop.

Even better, you can actually link an event handler to the on_change prop that will be called every time the date is updated:

API Reference

rx.moment

The Moment component.

PropType | ValuesDefault
interval
int
format
str
trim
bool
parse
str
add
MomentDelta
subtract
MomentDelta
from_now
bool
from_now_during
int
to_now
bool
with_title
bool
title_format
str
diff
str
decimal
bool
unit
str
duration
str
date
Union[str, datetime, date, time, timedelta]
duration_from_now
bool
unix
bool
local
bool
tz
str
locale
str

Event Triggers

See the full list of default event triggers
TriggerDescription
on_change Fires when the date changes.