Hey React Native developers! Today, let's delve into a common scenario we often encounter in mobile app development – subtracting days from a date. Whether you're working on a task scheduler or handling date-based logic.
This step-by-step guide will walk you through the process of subtracting days in a React Native project using Expo. Buckle up as we explore a straightforward solution to enhance your app's functionality.
In this article, we'll see how to subtract a day from a date object, react native subtract days in date and subtract day in moment js calendar react native.
Assuming you have Expo installed, create a new project using the following command:
expo init SubtractDaysDemo
cd SubtractDaysDemo
Install Moment.js using:
npm install moment
Add a function to manipulate the date by adding days.
import React,{useState} from 'react';
import { StyleSheet, Text, View} from 'react-native';
import moment from 'moment'
export default function App() {
return (
<View style={styles.container}>
<Text style={styles.date}>{moment().subtract(5, 'days').calendar() }</Text>
</View>
);
}
const styles = StyleSheet.create({
container: {
flex: 1,
backgroundColor: '#fff',
alignItems: 'center',
justifyContent: 'center',
},
text: {
fontSize: 24,
},
date: {
fontSize: 24,
fontWeight: 'bold',
padding: 15
}
});
Run your Expo app using:
expo start
Well, there you have it – a straightforward journey into subtracting days from a date in React Native using Expo. So go ahead, implement this knowledge in your projects, and keep exploring the vast landscape of React Native development. Happy coding!
You might also like:
- Read Also: How to Add Days in Date in React Native Calendar
- Read Also: How to Create Moment JS Calendar in React Native
- Read Also: How to Create Login Form using Spring Boot React
- Read Also: Laravel 10 React Auth Scaffolding Example