How to get Current Date Time in React Native
This is an example, we will see how to get the current date-time in React Native using the Date function directly and using the moment.js
which is very helpful when you deal with the date and time.
Moment.js is used to parse, validate, manipulate, and display dates and times in JavaScript
How to Get Current Date and Time using Date Function
To get the current date-time using Date function you just need to call it in the following ways
var date = new Date().getDate(); //To get the Current Date
var month = new Date().getMonth() + 1; //To get the Current Month
var year = new Date().getFullYear(); //To get the Current Year
var hours = new Date().getHours(); //To get the Current Hours
var min = new Date().getMinutes(); //To get the Current Minutes
var sec = new Date().getSeconds(); //To get the Current Seconds
How to Get Current Time using moment.js
var date = moment()
.utcOffset('+05:30')
.format('YYYY-MM-DD hh:mm:ss a');
moment()
will return the current date-timeutcOffset
is used to set your time zoneformat
will decide the output format- For Time Format:
- Y/YYYY -> Year
- M -> Month in single-digit(Ex. 8)
- MM -> Month in double-digit(Ex. 08)
- MMM -> Month short name (Ex. Aug)
- MMMM -> Month full name (Ex. August)
- D -> Day in single-digit(Ex. 5)
- DD -> Day in single double-digit(Ex. 05)
- HH -> Hours in 24 hr format
- hh -> Hours in 12 hr format
- mm -> Minutes
- ss -> Seconds
- a -> am/pm
You can use either Date function or moment.js to get the Date Time but as per my personal experience I would suggest using Date function while you are dealing in 24hr formate and if you are using 12hr (am/pm) format then go for moment.js. So let’s start with the example, we will see the one by one example for both.
First of all, you need to create a project
To Make a React Native App
Getting started with React Native will help you to know more about the way you can make a React Native project. We are going to use react native command line interface to make our React Native App.
If you have previously installed a global react-native-cli package, please remove it as it may cause unexpected issues:
npm uninstall -g react-native-cli @react-native-community/cli
Run the following commands to create a new React Native project
npx react-native init ProjectName
If you want to start a new project with a specific React Native version, you can use the --version argument:
npx react-native init ProjectName --version X.XX.X
Note If the above command is failing, you may have old version of react-native
or react-native-cli
installed globally on your pc. Try uninstalling the cli and run the cli using npx.
This will make a project structure with an index file named App.js in your project directory.
To get the Date Time Using Date Function
Open App.js in any code editor and replace the code with the following code.
App.js
// React Native Get Current Date Time
// https://aboutreact.com/react-native-get-current-date-time/
// import React in our code
import React, {useState, useEffect} from 'react';
// import all the components we are going to use
import {SafeAreaView, StyleSheet, View, Text} from 'react-native';
const App = () => {
const [currentDate, setCurrentDate] = useState('');
useEffect(() => {
var date = new Date().getDate(); //Current Date
var month = new Date().getMonth() + 1; //Current Month
var year = new Date().getFullYear(); //Current Year
var hours = new Date().getHours(); //Current Hours
var min = new Date().getMinutes(); //Current Minutes
var sec = new Date().getSeconds(); //Current Seconds
setCurrentDate(
date + '/' + month + '/' + year
+ ' ' + hours + ':' + min + ':' + sec
);
}, []);
return (
<SafeAreaView style={{flex: 1}}>
<View style={styles.container}>
<View style={styles.container}>
<Text style={styles.textStyle}>
Current Date Time
</Text>
<Text style={styles.textStyle}>
{currentDate}
</Text>
</View>
<Text
style={{
fontSize: 18,
textAlign: 'center',
color: 'grey'
}}>
React Native Get Current Date Time
</Text>
<Text
style={{
fontSize: 16,
textAlign: 'center',
color: 'grey'
}}>
www.aboutreact.com
</Text>
</View>
</SafeAreaView>
);
};
const styles = StyleSheet.create({
container: {
flex: 1,
backgroundColor: 'white',
justifyContent: 'center',
padding: 10,
},
textStyle: {
textAlign: 'center',
fontSize: 18,
color: 'black',
},
});
export default App;
To Run the React Native App
Open the terminal again and jump into your project using.
cd ProjectName
1. Start Metro Bundler
First, you will need to start Metro, the JavaScript bundler that ships with React Native. To start Metro bundler run following command:
npx react-native start
Once you start Metro Bundler it will run forever on your terminal until you close it. Let Metro Bundler run in its own terminal. Open a new terminal and run the application.
2. Start React Native Application
To run the project on an Android Virtual Device or on real debugging device:
npx react-native run-android
or on the iOS Simulator by running (macOS only)
npx react-native run-ios
Output Screenshots
Output in Online Emulator
This is how you can get the date-time using Date function. Now to get the date-time using moment.js
To get the Date Time Using moment.js
To use moment
you need to install moment
dependency.
Installation of Dependency
To install this dependency open the terminal and jump into your project
cd ProjectName
Run the following commands
npm install moment --save
Open App.js in any code editor and replace the code with the following code
App.js
// React Native Get Current Date Time | Use of Moment for Date Time
// https://aboutreact.com/react-native-get-current-date-time/
// import React in our code
import React, {useState, useEffect} from 'react';
// import all the components we are going to use
import {SafeAreaView, StyleSheet, View, Text} from 'react-native';
//Import moment for date and time
import moment from 'moment';
const App = () => {
const [currentDate, setCurrentDate] = useState('');
useEffect(() => {
var date = moment()
.utcOffset('+05:30')
.format(' hh:mm:ss a');
setCurrentDate(date);
}, []);
return (
<SafeAreaView style={{flex: 1}}>
<View style={styles.container}>
<View style={styles.container}>
<Text style={styles.textStyle}>
Current Date Time
</Text>
<Text style={styles.textStyle}>
{currentDate}
</Text>
</View>
<Text
style={{
fontSize: 18,
textAlign: 'center',
color: 'grey'
}}>
React Native Get Current Date Time
</Text>
<Text
style={{
fontSize: 16,
textAlign: 'center',
color: 'grey'
}}>
www.aboutreact.com
</Text>
</View>
</SafeAreaView>
);
};
const styles = StyleSheet.create({
container: {
flex: 1,
backgroundColor: 'white',
justifyContent: 'center',
padding: 10,
},
textStyle: {
textAlign: 'center',
fontSize: 18,
color: 'black',
},
});
export default App;
To Run the React Native App
Open the terminal again and jump into your project using.
cd ProjectName
1. Start Metro Bundler
First, you will need to start Metro, the JavaScript bundler that ships with React Native. To start Metro bundler run following command:
npx react-native start
Once you start Metro Bundler it will run forever on your terminal until you close it. Let Metro Bundler run in its own terminal. Open a new terminal and run the application.
2. Start React Native Application
To run the project on an Android Virtual Device or on real debugging device:
npx react-native run-android
or on the iOS Simulator by running (macOS only)
npx react-native run-ios
Output Screenshots
Output in Online Emulator
This is how you can get the current date and time using Date Function in 24hr format and using moment.js in 12hr format in React Native. If you have any doubts or you want to share something about the topic you can comment below or contact us here. There will be more posts coming soon. Stay tuned!
Hope you liked it. 🙂
hellow
can i get like this 17-Nov-2019
Please update the following code in the example
can i get like this
wed / 7pm
setCurrentDate(moment().utcOffset('+05:30').format('ddd/hh a'));
var date = new Date().getDay(); is giving the same result as var date = new Date().getDate(); so how can i have sunday, monday etc
thanks
You can use
format('ddd')