3

I'm trying to get the current day/month/year in a React Native App. I am new at this, so I have many doubts about the implementation.

I tried to follow the following solution, but does not work for me:

currentDay: new Date(),

console.log('Date:'+this.state.currentDay ,);
console.log('Day: '+this.state.currentDay.getDay() , 'Month: ' + this.state.currentDay.getMonth(), 'Year :'+ this.state.currentDay.getYear());

4 Answers4

5

Most easiest way to install npm install moment --save

Moment is a JavaScript date and time manipulation library for parsing, validating, manipulating, and formatting dates. 

You can simply import it in your code:

import moment from 'moment'; 
var now = moment().format();

if you just want current date and output in your desired format, you can use moment with format function

var currentDate = moment().format("DD/MM/YYYY");

Result

30/03/2019

You can change date format according to your requirement using Moment library

You can refer the following documents read about how to use moment and what function they will provide

https://momentjs.com

https://devhints.io/moment

Vishal Dhanotiya
  • 2,512
  • 1
  • 13
  • 32
2

Install the moment package: npm i moment

import moment from 'moment';

var dateAndTime= moment().format("DD/MM/YYYY HH:mm:ss")

output: 15/11/2019 15:33:23

var date= moment().format("DD/MM/YYYY")

output: 15/11/2019

(this output is only for current date, of course).

robsiemb
  • 6,157
  • 7
  • 32
  • 46
Nithin Naidu
  • 151
  • 1
  • 4
1
const onPressDateValidation = () =>{

    const currentDate ='2022-02-20'
  //  const currentDate ='2022-02-25'

  // const currentDate ='2022-02-26'

    const fromDate ='2022-02-20'
    const toDate = '2022-02-25'
   
    console.log("Umesh_______1 ", currentDate);
    console.log("Umesh_______2 ", fromDate);
   
    if ( moment(currentDate).isSameOrAfter(fromDate) ) {
      if( moment(currentDate).isSameOrBefore(toDate)){
        alert("Show your Ad either it's Banner Ad or Video Ad")
      }else{
        alert(" Ad is Expired")
      }
    } else {
      alert("Ad is not started yet")
    }
  }
Umesh Gera
  • 31
  • 2
0

You need to add a getDate() function after newDate() .. i.e. var date = new Date.getDate()

The following code example (which should clarify implementation purposes) is illustrated on AboutReact.com, where you can run the code online.

import React, { Component } from 'react';

import { StyleSheet, View, Alert, Text } from 'react-native';

export default class App extends Component {
  constructor(props) {
    super(props);
    this.state = {
      //default value of the date time
      date: '',
    };
  }
  componentDidMount() {
    var that = this;

    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

    that.setState({
      //Setting the value of the date time
      date:
        date + '/' + month + '/' + year + ' ' + hours + ':' + min + ':' + sec,
    });
  }

  render() {
    return (
      <View
        style={{
          flex: 1,
          justifyContent: 'center',
          alignItems: 'center',
        }}>
        <Text
          style={{
            fontSize: 20,
          }}>
          Current Date Time
        </Text>
        <Text
          style={{
            fontSize: 20,
            marginTop: 16,
          }}>
          {this.state.date}
        </Text>
      </View>
    );
  }
}

Obviously, the style is flexible and you could omit the time element of the code sample (but it may be useful to other users)..

Hope this helps

Rachel Gallen
  • 27,943
  • 21
  • 72
  • 81