Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get the Current Date in ReactNative?

I am building my first ReactNative iOS and Android app. I am an iOS coder with Swift and Obj-C. How do I fetch the current date using ReactNative.

Shall I use Native Modules or is there an ReactNative API from which I can get this data ?

like image 975
Gautham Pai Avatar asked May 17 '16 08:05

Gautham Pai


People also ask

How can I get today's date in react native?

To get the current date in react native has the Date() object it will return every time the current date and time when we use it. you have just call it like new Date() . we have to just write new Date() and it returns the date object where we get a year, month, date, hours, minutes, and seconds.

How do you get the current month in react native?

To get the current month in react native has the new Date(). getMonth() method which will return every time the current month index in number datatype. we have to just call new Date(). getMonth() and it will check the current date and return month index based on the date.

How do I show the date as text in react native?

To format a date string in React Native, we can use moment. js. to call moment with a JavaScript date object to create a moment object with the same date. Then we call format with a format string to return a date string with the given date.

How do I find the current moment of a date?

To get current date with Moment and JavaScript, we use the moment function without any arguments. const datetime = moment();


1 Answers

The JavaScript runtime in React Native has access to date information just like any other environment. As such, simply:

new Date() 

... will give you the current date. Here's the MDN on working with dates in JS.

If you want a good JS library to make working with dates even easier, check out MomentJS.

like image 67
Adam Terlson Avatar answered Sep 21 '22 13:09

Adam Terlson