如何在 React Native 中获取当前日期?
How to get the Current Date in ReactNative?
我正在构建我的第一个 ReactNative iOS 和 Android 应用程序。我是一名 iOS 程序员,使用 Swift 和 Obj-C。如何使用 ReactNative 获取当前日期。
我应该使用 Native Modules 还是有 ReactNative API 我可以从中获取这些数据?
React Native 中的 JavaScript 运行时可以像任何其他环境一样访问日期信息。因此,简单地说:
new Date()
... 将为您提供当前日期。 Here's the MDN 关于在 JS 中处理日期。
如果您想要一个好的 JS 库来更轻松地处理日期,请查看 MomentJS。
如果你得到当前milisec
日期使用+ new Date()
如果得到当前使用new Date()
希望对你有帮助
好运
我使用 {new Date()}
生成 Invariant Violation: Objects are not valid
错误,以下日期函数对我有用。
const getCurrentDate=()=>{
var date = new Date().getDate();
var month = new Date().getMonth() + 1;
var year = new Date().getFullYear();
//Alert.alert(date + '-' + month + '-' + year);
// You can turn it in to your desired format
return date + '-' + month + '-' + year;//format: dd-mm-yyyy;
}
更多信息https://reactnativecode.com/get-current-date-react-native-android-ios-example
它也适用于 ios。
我认为这应该可行;
new Date().toLocaleString()
它将return日期和时间格式化为您当地的格式(通常为以下格式);
"6/22/2021, 2:59:00 PM"
我正在构建我的第一个 ReactNative iOS 和 Android 应用程序。我是一名 iOS 程序员,使用 Swift 和 Obj-C。如何使用 ReactNative 获取当前日期。
我应该使用 Native Modules 还是有 ReactNative API 我可以从中获取这些数据?
React Native 中的 JavaScript 运行时可以像任何其他环境一样访问日期信息。因此,简单地说:
new Date()
... 将为您提供当前日期。 Here's the MDN 关于在 JS 中处理日期。
如果您想要一个好的 JS 库来更轻松地处理日期,请查看 MomentJS。
如果你得到当前milisec
日期使用+ new Date()
如果得到当前使用new Date()
希望对你有帮助
好运
我使用 {new Date()}
生成 Invariant Violation: Objects are not valid
错误,以下日期函数对我有用。
const getCurrentDate=()=>{
var date = new Date().getDate();
var month = new Date().getMonth() + 1;
var year = new Date().getFullYear();
//Alert.alert(date + '-' + month + '-' + year);
// You can turn it in to your desired format
return date + '-' + month + '-' + year;//format: dd-mm-yyyy;
}
更多信息https://reactnativecode.com/get-current-date-react-native-android-ios-example 它也适用于 ios。
我认为这应该可行;
new Date().toLocaleString()
它将return日期和时间格式化为您当地的格式(通常为以下格式);
"6/22/2021, 2:59:00 PM"