Skip to main content

React Native + Javascript Date Time manipulation: moment and date-fns libraries

 There are multiple method to format and manipulate Date and Time. Generally speaking the most common libraries used to manipulate dates are Day.js, Luxon, Moment, and Date-fns.

Moment developers have discouraged developers to use moments at this time due to not being built for modern JavaScript era.

Day.js

Installation:

npm install dayjs --save

Header import:

import calendar from 'dayjs/plugin/calendar'
import dayjs from "dayjs";

Usage example:

dayjs.extend(calendar) dayjs().calendar(dayjs('2008-01-01')) dayjs().calendar(null, { sameDay: '[Today at] h:mm A', // The same day ( Today at 2:30 AM ) nextDay: '[Tomorrow at] h:mm A', // The next day ( Tomorrow at 2:30 AM ) nextWeek: 'dddd [at] h:mm A', // The next week ( Sunday at 2:30 AM ) lastDay: '[Yesterday at] h:mm A', // The day before ( Yesterday at 2:30 AM ) lastWeek: '[Last] dddd [at] h:mm A', // Last week ( Last Monday at 2:30 AM ) sameElse: 'DD/MM/YYYY' // Everything else ( 17/10/2011 ) })

Additional links: https://day.js.org/docs/en/plugin/calendar

Formats

https://day.js.org/docs/en/display/format

Luxon usage

npm install --save luxon

Luxon has great date time support. However, relative calendar outputs will change to in x days format - which I don't like.

 DateTime.fromJSDate(date).toRelativeCalendar();

Moment usage

import moment from "moment";

And to use the moment library, use the method below (note the bolded text). In the example below, I use calendar function to get the relative date to output on my application.

let formattedDate = moment(date).calendar(null, {

            sameDay: '[Today]',

            nextDay: '[Tomorrow]',

            nextWeek: 'dddd',

            lastDay: '[Yesterday]',

            lastWeek: '[Last] dddd',

            sameElse: ' '

        });

Further examples: https://betterprogramming.pub/using-moment-js-in-react-native-d1b6ebe226d4

Date-fns

This date-fns library is a lot lighter to use since you can import a specific function instead of the entire large library. However, it doesn't have certain customized option to allow manipulation of the relative date output similar to above.

Import the function to use similar to below:

import { format, formatRelative } from "date-fns";

To use in the code, you call the simple format date function similar to below:

format(date, "MMM d, yyyy");

Comments

Popular posts from this blog

Checked Task Pro Instruction (BlackBerry 10)

  Checked Instruction I just started working on this page, please send me comments and suggestions that could improve this site. Please note that if you use the Android app, the instruction is similar to the BlackBerry app below. I will create a separate instruction page for Android app soon. Contents 1  Initial Set-up after Installation 2  SMART task system that combine the best of GTD (Getting Things Done) and MYN  3  Smart Add - natural language date parser 4  ***NEW*** Shortcuts 5  Back-up and Restore - How to Transfer data from one device to another 6  BES - Work Space - Add a public app from the BlackBerry World storefront 7  Settings options that will definitely increase your productivity 8  Automatically add email as task 9  Tips on improving performance 10  Add a category 11  Delete a category 12  Keep categories permanently 13  Add sub-task 14  Automatic Synchronize from remember 15  Sort opti...

Checked! Task Pro - Blackberry Instruction

  Checked Instruction I just started working on this page, please send me comments and suggestions that could improve this site. Please note that if you use the Android app, the instruction is similar to the BlackBerry app below. I will create a separate instruction page for Android app soon. Contents 1  Initial Set-up after Installation 2  SMART task system that combine the best of GTD (Getting Things Done) and MYN  3  Smart Add - natural language date parser 4  ***NEW*** Shortcuts 5  Back-up and Restore - How to Transfer data from one device to another 6  BES - Work Space - Add a public app from the BlackBerry World storefront 7  Settings options that will definitely increase your productivity 8  Automatically add email as task 9  Tips on improving performance 10  Add a category 11  Delete a category 12  Keep categories permanently 13  Add sub-task 14  Automatic Synchronize from remember 15  Sort opti...

Setting up Mac for Windows users (users that need to be use both Mac + Windows)

My personal usage... please make sure the software you use is safe prior to downloading: AltTab -> Switching windows Change keyboard short to use Control vs Command Mouse setting: disable natural scrolling (make it similar to windows scrolling) Safari View -> Show Favorites Bar App Store: download ad blocker.. I use AdGuard but people said good things about Ghostery BetterTouchControl ( https://boastr.net/ ) Setup back and forward Logitech button (or any other mouse). Normal Mice Tab: Add Key and select the key to replaceBack and Forward button control Terminal: setup Control + C to break the current command (instead of Command + C)