How to use Date() with inputs in Expo Version 48, working in normal JS file but not in project?

153 views Asked by At

I have made project with Expo version 48 and Date() is not working. In the below code "directDate" was working completely fine in Expo version 47 or separate javascript file.

Can anyone explain how to use Date() in expo version 48.

I am beginner and tried this for 7 hours to solve this, please help, thanks in advance.

App.js

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

export default function App() {
  const inputDate = "2023/05/20";
  const directDate = new Date(inputDate); // Getting Invalid Date ?


  return (
    <View style={styles.container}>
      <Text>Try Date - {directDate.toDateString()}</Text>
    </View>
  );
}

const styles = StyleSheet.create({
  container: {
    flex: 1,
    backgroundColor: "#fff",
    alignItems: "center",
    justifyContent: "center",
  },
});

1

There are 1 answers

0
Caio Augusto On

In JS normally your Date uses dash instead of slash.

const inputDate = "2023-05-20";
const directDate = new Date(inputDate);

This date should now work as intended.