How To Get Current Date And Time In React JS

Websolutionstuff | Sep-02-2022 | Categories : React JS

In this article, we will see how to get the current date and time in react js. You can get the current date and time using react js. We will use the date() function to get the current timestamp. Also, you can get the current month and current year in react js. In react js date function return current date yyyy-mm-dd.

So, let's see how to get the current date in react js, react js get the current date and time.

  • React Get Current Date Time
  • React Get Current Date (Y-m-d)
  • React Get Current Month
  • React Get Current Year
  • React Get Current Time (H:i:s)
React Get Current Date Time

 In this example, we will get the day name with date and time and also get the timezone.

import React, { Component } from 'react';
import { render } from 'react-dom';
   
class App extends Component {
  constructor() {
    this.state = {
      currentDateTime: Date().toLocaleString()
    }
  }
  
  render() {
    return (
      <div>
        <p>
          { this.state.currentDateTime }
        </p>
      </div>
    );
  }
}
  
render(<App />, document.getElementById('root'));

Output:

Sat Aug 27 2020 10:30:56 GMT+0530 (India Standard Time)

 

 

React Get Current Date (Y-m-d)

In this example, we will get the Y-m-d format date.

import React, { Component } from 'react';
import { render } from 'react-dom';
   
class App extends Component {
  constructor() {
   
    var today = new Date(),
    date = today.getFullYear() + '-' + (today.getMonth() + 1) + '-' + today.getDate();
   
    this.state = {
      currentDate: date
    }
  }
   
  render() {
    return (
      <div>
        <p>
          { this.state.currentDate }
        </p>
      </div>
    );
  }
}
   
render(<App />, document.getElementById('root'));

Output:

2022-08-27

 

React Get Current Month

In this example, we will get the current month using the getMonth() function.

import React, { Component } from 'react';
import { render } from 'react-dom';
  
class App extends Component {
  constructor() {
    this.state = {
      currentMonth: new Date().getMonth()
    }
  }
   
  render() {
    return (
      <div>
        <p>
          { this.state.currentMonth }
        </p>
      </div>
    );
  }
}
render(<App />, document.getElementById('root'));

Output:

8

 

 

React Get Current Year

In this example, we will get the current year using the getFullYear() function.

import React, { Component } from 'react';
import { render } from 'react-dom';
   
class App extends Component {
  constructor() {
    this.state = {
      currentYear: new Date().getFullYear()
    }
  }
   
  render() {
    return (
      <div>
        <p>
          { this.state.currentYear }
        </p>
      </div>
    );
  }
}
   
render(<App />, document.getElementById('root'));

Output:

2022

 

React Get Current Time (H:i:s)

In this example, we will currently time using the javascript function.

import React, { Component } from 'react';
import { render } from 'react-dom';
   
class App extends Component {
  constructor() {
   
    var today = new Date(),
    time = today.getHours() + ':' + today.getMinutes() + ':' + today.getSeconds();
   
    this.state = {
      currentTime: time
    }
  }
   
  render() {
    return (
      <div>
        <p>
          { this.state.currentTime }
        </p>
      </div>
    );
  }
}
render(<App />, document.getElementById('root'));

Output:

10:30:57

 


You might also like:

Recommended Post
Featured Post
How To Validate Username And Password In React JS
How To Validate Username And P...

In this article, we will see how to validate username and password in react js. In this example, we will validate t...

Read More

Sep-13-2022

Laravel 9 Insert Multiple Records In Database
Laravel 9 Insert Multiple Reco...

In this article, we will see laravel 9 insert multiple records in the database. Here, we will learn how to ins...

Read More

Dec-16-2022

How To Import Export Excel & CSV File In Laravel 10
How To Import Export Excel & C...

In this article, we will see how to import and export Excel & CSV files in laravel 10. Here, we will learn about lar...

Read More

Mar-08-2023

How To Upload Multiple Image In Laravel 9
How To Upload Multiple Image I...

 In this article, we will see how to upload multiple images in laravel 9. here, we will see the tutorial of multipl...

Read More

Mar-18-2022