Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/466.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/date/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
javascript如何更改date now格式_Javascript_Date_Datetime - Fatal编程技术网

javascript如何更改date now格式

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

我面临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 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'}`);