Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to subtract and plus date in react native

Tags:

react-native

Actually, i am stuck in one problem that is about Date.

I want to subtract and plus the date in react native.

is there any solution about it.

I have used Moment.js but the error occurs in it so please help me.

Thank you in advance

like image 213
Swapnil Panchal Avatar asked Dec 05 '22 13:12

Swapnil Panchal


1 Answers

check below working example of add and subtract day, month and date using moment:

For Day

let tomorrow = new Date();
tomorrow = moment(tomorrow).add(1, 'day').format('YYYY-MM-DD'); // for specific format

let today = moment(new Date()).format('YYYY-MM-DD');

let tomorrow = new Date();
tomorrow = moment(tomorrow).add(1, 'day').format('YYYY-MM-DD');

For Month

moment(currentMonth).add(1, 'month');
moment(currentMonth).subtract(1, 'month');

For Date

let now  = "14/11/2016";
let then = "03/12/2017";

let diff = moment(now,"DD/MM/YYYY").diff(moment(then,"DD/MM/YYYY"));
let duration = moment.duration(diff);
let result = duration.format("hh:mm:ss");

Please refer detailed date difference: https://stackoverflow.com/a/18624295/6592263

EDIT (Not tested)

To get Month

let d = moment(new Date());
d.month(); // month number
d.format('ddd MMM DD YYYY');

TO get Year

let d = moment(new Date());
d.year() // year
like image 172
Jigar Shah Avatar answered Feb 07 '23 12:02

Jigar Shah