javascript如何更改date now格式
我面临javascript日期的问题。我想更改日期的格式javascript如何更改date now格式,javascript,date,datetime,Javascript,Date,Datetime,我面临javascript日期的问题。我想更改日期的格式 this.setState({ current: Date(Date.now()), }, 1000); //convert minutes //if minutes are 0 to 29 then show current hours reset the minutes again start with 0 like 18:00 //if minutes are 29
this.setState({
current: Date(Date.now()),
}, 1000);
//convert minutes
//if minutes are 0 to 29 then show current hours reset the minutes again start with 0 like 18:00
//if minutes are 29 to 59 then show current hours reset the minutes again start with 30 like 18:30
var slotTime1 = currentdate.getHours() +':'+ (currentdate.getMinutes() <= 29 ? '00' : '30') ; //10:30
期望
10:00:52 AM
我应该更改什么?您可以简单地使用日期方法,如:
const current=新日期()
const timestring=current.toLocaleTimeString()
console.log(timestring)/=>10:47:52 AM
var date=new date();
console.log(date.getHours()+”:“+date.getMinutes()+”:“+date.getSeconds()+”+date.getHours()使用。当作为函数调用时,date不带任何参数并返回表示当前日期和时间的字符串。因此在date(date.now())中
,参数是多余的,表达式产生的结果与Date()
完全相同。它不起作用,我在setState
中定义了它。我已更新了有关如何使用setState
的代码。我应该如何在setState中定义我的current.toLocaleTimeString()
,如下:this.setState({current:new Date().toLocaleTimeString([],{hour:'2-digital',minute:'2-digital'});
你不理解日期。getHours()<12?'AM':'PM'
的输入更少,语义更丰富。你还应该将分和秒填充到两位数。;-)
10:00:52 AM
10:30 AM
var date=new Date();
console.log(date.getHours() + ":" + date.getMinutes()+ ":" + date.getSeconds() + " " + date.getHours()<12 ? 'AM' : 'PM'}`);