我试图获取时间和日期,并将其设置为本地设备格式。
我试过这个api "https://www.npmjs.com/package/react-native-device-time-format?activeTab=readme",
但根本不工作..。
这是我的密码,你能看一下吗?太感谢你了?
import React,{useEffect,useState} from 'react';
import { StyleSheet, View } from 'react-native';
import { is24HourFormat } from 'react-native-device-time-format';
import moment from 'moment';
function TryTime(props) {
const [currentTime, setCurrentTime] = useState("");
const getCurrentHourFormat = async (date) => {
const is24Hour = await is24HourFormat();
return moment(date).format(is24Hour ? 'HH:mm' : 'h:mm A');
}
useEffect(() => {
(async () => {
const timeNow = await TryTime(Date.now);
setCurrentTime(getCurrentHourFormat(timeNow));
})();
}, []);
return (
<View style={styles.container}>
<Text>{currentTime}</Text>
</View>
);
}
const styles = StyleSheet.create({
container : {
flex : 1,
justifyContent : 'center',
alignItems : 'center',
}
})
export default TryTime;发布于 2021-04-26 07:37:50
首先,要通过JS获取设备的本地时间,您可以使用js:toLocaleString()注意到的内置函数:
关于这个问题:你知道如何变得更简单吗?大约1秒前,10分钟前,1小时前
=>我想你有以前的约会时间,你想把它和现在的时间进行比较
类似于:
a = ... // your previous time
b = new Date().toLocaleString()只需将let c = b - a作为两个日期之间的差额,然后
c / 1000 => get seconds
c / 1000 * 60 => get minutes
c / 1000 * 3600 => get hourshttps://stackoverflow.com/questions/67258707
复制相似问题