How to get the time difference in react native - react-native

Is there any React-native library that helps getting the difference between two dates. I mean like the following.
Example

try using moment
import moment from 'moment';
{
moment(date, 'YYYY-MM-DD')
.month(0)
.from(moment().month(0))
}
'YYYY-MM-DD' can be whatever format
this one is getting me the difference between the given date and now
here you can find documentation https://momentjs.com/docs/
and here more libraries https://blog.logrocket.com/4-alternatives-to-moment-js-for-internationalizing-dates/

Related

is there any date picker package in react-native which supports entering date manually as shown in the image?

Usually date pickers allow users to select any date from the calendar. Besides to that I want to allow user to enter date manually as shown in the image.
Can anybody suggests any RN package that suits my requirement?. Thank You
I would use a masked text input.
Try using this iMask.js - it's very powerful!
https://imask.js.org
With this react-native wrapper for it:
https://github.com/uNmAnNeR/imaskjs/tree/master/packages/react-native-imask
Then it's just a matter of:
<IMaskTextInput
value={someValue}
mask={Date}
min={new Date(1990, 0, 1)}
max={new Date()} // today
lazy={false}
onAccept={(value, mask) => console.log(value)}
/>
See docs of both libraries for more config details.
You can easily extend your own custom TextInput with masking functionality using this wrapper.

React Native - Customize date format using Moment.js

I am using moment.js to get the current date, however I am having a problem about the date formatting and I don't have any idea on how to customize it since I am just new using it.
By the way I used this.
moment().format('LLLL');
My expected output is:
Mmmm-dd-yyyy hh:mmAM/PM (Timezone) or just like Sep-19-2020 3:00AM (GMT +8)
Is the any other way to achieve this?
moment().locale('tr').format("MMM-DD-yyyy h:mA [(GMT] Z[)]")
moment().locale('en_US').format("MMM-DD-yyyy h:mA [(GMT] Z[)]")
If you want to use locale in React Native.
instead
import moment from 'moment'
you should use this
import 'moment/min/moment-with-locales'
moment.locale('tr');
moment().format("MMM-DD-yyyy h:mA [(GMT] Z[)]");
The things you write in square brackets are written directly. that is, they are escape characters. 

assign date value into native base datepicker

Using nativebase datepick:
this.state.date value is 2019-11-15T00:00:00+00:00
<DatePicker
defaultDate={
this.state.date
? new Date(this.state.date) : null
problem is the datepicker show 14 November 2019, because of converting to phone timezone.
I can also use momentjs library.
I think moment.utc(yourDateHere)could help you :)
Here is an example of how it works:
Moment dates example

react-native : determine whether a point falls on or near a polyline

I want to determine whether a point falls on or near a polyline
im using react native as platform , and the package : react-native-google-maps to provide the map
i did some search and as a result i found this function ' isLocationOnEdge() ' in Geometry Library of google link :
Geometry Livrary
that did the same function as i want
the problém is that i can't implement this function in react-native
any help ?
Have a look at Turf.js. I used it in a RN project with react-native-maps for geo calculations.
booleanPointOnLine solves part one of your problem (on a line),
pointToLineDistance solves part two (near a line).
Add to package.json
"dependencies": {
"#turf/turf": "^5.1.6"
}
Import in component
import { pointToLineDistance } from '#turf/point-to-line-distance';
import { booleanPointOnLine} from '#turf/boolean-point-on-line';
Use as described in the docs.

React Native moment.locale does not work

I am working with React Native version 0.45.1 and with moment version 2.18.1.
I am trying to change the date according to the device local, but I always get the date in 'en-Us' locale.
I can't import all of the locales as I saw in some solutions since I don't know the device locale in advance.
(for example: https://github.com/moment/moment/issues/2962)
any other options?
I can't import all of the locales as I saw in some solutions since I don't know the device locale in advance.
Actually, you can import all of the locales in moment like this (moment-with-locales is mentioned right there on the homepage):
import moment from 'moment/min/moment-with-locales'
// Or if you are using require instead:
var moment = require('moment/min/moment-with-locales')
Then you should be able to get your device locale with whatever module/method you prefer (in my example, I'll be using Expo) and change the moment locale to it. For example:
var deviceLocale = await Expo.Util.getCurrentLocaleAsync()
moment.locale(deviceLocale)
I won't say that importing everything is the best method for handling this as moment-with-locales is larger than just moment, but it does what you want it to accomplish. You can also go the route of just importing the locales you support as mentioned in that Github comment I linked to.
Instead of importing locale by locale, I'm using this solution to set the locale globally:
import { getDeviceLocale } from "react-native-device-info";
import moment from "moment";
import "moment/min/locales";
const deviceLocale = getDeviceLocale();
moment.locale(deviceLocale); //set your locale (eg: fr)
moment(1316116057189).fromNow(); // il y a 7 ans
Sharing this to those who need it