Skip to main content

How to Get Current Date & Time in React


useEffect(() => {
const dateString = Date.now()
    const formatDate = (dateString) => {
      const options = { year: "numeric", month: "long", day: "numeric"}
      return new Date(dateString).toLocaleString(undefined, options)
    }
    alert(JSON.stringify(formatDate(dateString)))
}, [])

------------------------------------------------------------------------------------------------------------------

If don't want to use options and just want to use dd/mm/yyyy format then use below code.

const dateString = Date.now()

    const formatDate = (dateString) => {

      return new Date(dateString).toLocaleString('en-GB')

    }

alert(JSON.stringify(formatDate(dateString)))

Comments

Popular posts from this blog

OTP verification 2 minutes count down timer

Java Script  let timerOn = true; function timer(remaining) {   var m = Math.floor(remaining / 60);   var s = remaining % 60;   m = m < 10 ? '0' + m : m;   s = s < 10 ? '0' + s : s;   document.getElementById('timer').innerHTML = m + ':' + s;   remaining -= 1;   if(remaining >= 0 && timerOn) {     setTimeout(function() {         timer(remaining);     }, 1000);     return;   }   if(!timerOn) {     // Do validate stuff here     return;   }   // Do timeout stuff here   alert('Timeout for otp'); } // Then Call the function of Timer // timer(120); HTML <input type="text" name="otp" placeholer="Enter your otp"/> <div>Time left = <span id="timer"></span></div>
Error: [Reanimated] valueUnpacker is not a worklet, js engine: hermes Fixed it with a version downgrade: npm install react-native-reanimated@3.4.2 And editing the babel.config.js file: module.exports = {   presets: ['module:metro-react-native-babel-preset'],   plugins: ['react-native-reanimated/plugin'], }; 
Prevent SweetAlert to be closed on clicking outside the popup window If you are using Sweet Alert 2, you can use this configuration allowOutsideClick: false, To stop closing by escape key use below also: allowEscapeKey: false,