Javascript react native:如何显示16:02:08而不是16:2:8这样的时间?
时间是怎样显示的呢?因为现在我看到的时间是这样的 我不明白那有什么问题。 在本例中,我尝试显示当前时间及其显示,不带“零”和“零” 我不明白为什么会这样。 我尝试了很多事情,但都不管用,我很乐意为这个问题提供帮助Javascript react native:如何显示16:02:08而不是16:2:8这样的时间?,javascript,reactjs,react-native,react-hooks,Javascript,Reactjs,React Native,React Hooks,时间是怎样显示的呢?因为现在我看到的时间是这样的 我不明白那有什么问题。 在本例中,我尝试显示当前时间及其显示,不带“零”和“零” 我不明白为什么会这样。 我尝试了很多事情,但都不管用,我很乐意为这个问题提供帮助 function NetuneyDigum() { const [currentTime, setCurrentTime] = useState(""); return ( <> <View style=
function NetuneyDigum() {
const [currentTime, setCurrentTime] = useState("");
return (
<>
<View
style={{
flex: 1,
alignItems: 'center',
backgroundColor: '#cbced4',
}}
>
<View
style={{
paddingTop: 30,
flexDirection: 'row',
paddingRight: 180,
}}
>
<View
style={{
flexDirection: 'row',
alignItems: 'center',
justifyContent: 'center',
left: 58,
}}
>
<Text
style={{
fontWeight: 'bold',
fontSize: 18,
color: 'black',
}}
>
THE TIME IS: {currentTime}
</Text>
</View>
<View
style={{
width: 150,
borderRadius: 5,
borderColor: 'black',
borderWidth: 2,
left: 80,
}}
></View>
<TouchableOpacity
onPress={()=>{
const today = new Date(),
time = today.getHours() + ':' + today.getMinutes() + ':' + today.getSeconds();
setCurrentTime(time);
}}
style={{
height: 60,
width: 60,
borderRadius: 5,
borderColor: 'black',
borderWidth: 2,
alignItems: 'center',
justifyContent: 'center',
backgroundColor: 'red',
left: 100,
}}
>
<Icon name="md-time" size={50} color="black" />
</TouchableOpacity>
</View>
</View>
</>
);
}
函数NetuneyDigum(){
const[currentTime,setCurrentTime]=useState(“”);
返回(
时间是:{currentTime}
{
const today=新日期(),
时间=今天。getHours()+':'+今天。getMinutes()+':'+今天。getSeconds();
setCurrentTime(时间);
}}
风格={{
身高:60,
宽度:60,
边界半径:5,
边框颜色:“黑色”,
边界宽度:2,
对齐项目:“居中”,
为内容辩护:“中心”,
背景颜色:“红色”,
左:100,,
}}
>
);
}
根据这篇文章,你可以这样使用它:
var d=新日期();
d、 toLocaleString();//->“2013年2月1日上午7:37:08”
d、 toLocaleDateString();//->"2/1/2013"
d、 toLocaleTimeString();//->“上午7:38:05”
下面是我的ReactJS倒计时组件,它具有您想要实现的倒计时属性。它只需要:
import React,{useState,useffect}来自“React”;
从'react-chartjs-2'导入{Doughnut};
导入“../css/timerComponent.css”
常数RightBar=()=>{
常数秒\u限制=90;
//左时间计算器
让finish=新日期();
finish.setSeconds(finish.getSeconds()+秒数限制);
常量[结束时间]=使用状态(完成);
常量calculateTimeLeft=()=>{
常量差=+结束时间-+新日期();
如果(差异>0)
收益差;
否则{
返回0
}
}
const[timeLeft,setTimeLeft]=useState(calculateTimeLeft());
常量checkTimeFunction=()=>{
设置超时(()=>{
setTimeLeft(calculateTimeLeft);
},1000);
};
//要做的事!!添加依赖项数组
useffect(()=>{
checkTimeFunction()
});
//图表
const[chartData,setChartData]=useState({});
useffect(()=>{
如果(时间限制){
常数图=()=>{
如果(时间限制){
setChartData({
数据集:
[
{
数据:[时间限制,秒数限制*1000-时间限制],
背景色:['#61daff'],
},
]
});
}
}
图表(时间限制);
}},[timeLeft]);
返回(
{timeLeft?Math.floor((timeLeft/(3600000))%24)+':'+Math.floor((timeLeft/60000)%60)+':'+Math.floor((timeLeft/1000)%60):'你的时间到了'}
);
}
导出默认右栏;
根据这篇文章,你可以这样使用它:
var d=新日期();
d、 toLocaleString();//->“2013年2月1日上午7:37:08”
d、 toLocaleDateString();//->"2/1/2013"
d、 toLocaleTimeString();//->“上午7:38:05”
下面是我的ReactJS倒计时组件,它具有您想要实现的倒计时属性。它只需要:
import React,{useState,useffect}来自“React”;
从'react-chartjs-2'导入{Doughnut};
导入“../css/timerComponent.css”
常数RightBar=()=>{
常数秒\u限制=90;
//左时间计算器
让finish=新日期();
finish.setSeconds(finish.getSeconds()+秒数限制);
常量[结束时间]=使用状态(完成);
常量calculateTimeLeft=()=>{
常量差=+结束时间-+新日期();
如果(差异>0)
收益差;
否则{
返回0
}
}
const[timeLeft,setTimeLeft]=useState(calculateTimeLeft());
常量checkTimeFunction=()=>{
设置超时(()=>{
setTimeLeft(calculateTimeLeft);
},1000);
};
//要做的事!!添加依赖项数组
useffect(()=>{
checkTimeFunction()
});
//图表
const[chartData,setChartData]=useState({});
useffect(()=>{
如果(时间限制){
常数图=()=>{
如果(时间限制){
setChartData({
数据集:
[
{
数据:[时间限制,秒数限制*1000-时间限制],
背景色:['#61daff'],
},
]
});
}
}
图表(时间限制);
}},[timeLeft]);
返回(
{timeLeft?Math.floor((timeLeft/(3600000))%24)+':'+Math.floor((timeLeft/60000)%60)+':'+Math.floor((timeLeft/1000)%60):'你的时间到了'}
);
}
导出默认右栏;
d.toLocaleTimeString();//->“7:38:05 AM”
对于这个简单的例子来说可能有些过分,但您可能想研究一下使用momentjs()之类的工具,它使您能够传递日期,并根据需要对其进行格式化(仅包括时间)。d.toLocaleTimeString();//->“7:38:05 AM”
对于这个简单的例子来说可能有些过分,但您可能想研究一下使用momentjs()之类的工具,它使您能够传递日期,并根据需要对其进行格式化(仅包括时间)。