Java脚本日期函数

问题描述 投票:0回答:1

下面的JS获取当前时间不返回任何值。我在我的jsp页面中调用ecmascript函数。

function abc{   
    var returnString = '';
    var today = new Date();
    var currentTime = today.getHours(); 
    return currentTime;
}
javascript ecmascript-5
1个回答
2
投票

这段代码可以工作。

function abc() {   
 var today = new Date();
 return  today.getHours(); 
}


display.log(abc())

1
投票

你缺少的是 () 为你的函数。


function abc(){   
    var today = new Date();
    var currentTime = today.getHours(); 
    return currentTime;
}

console.log(abc())

0
投票

函数声明错误。在函数名后面 () 应使用。请参考以下代码

function abc() {   
var returnString = '';
var today = new Date();
var currentTime = today.getHours(); 
return currentTime;
}

0
投票

你的函数声明是错误的。 你的函数声明是错误的。

function abc {

你需要列出参数,即使是一个空的列表,用这样的东西。

function abc () {

0
投票

有两件事

你缺少了 () 正如别人指出的那样。

其次,我不知道这个方法的目的是什么,但可以缩短。请看我的 abcd()

var a = abc();
var b = abcd();
console.log(a);
console.log(b);

function abc() {   
    var returnString = '';
    var today = new Date();
    var currentTime = today.getHours(); 
    return currentTime;
}

function abcd() {
  return new Date().getHours();
}

0
投票

一切都很好。你在函数声明中缺少了括号。取而代之的是 function abc{...} 你应该使用 function abc(){...}. 而要显示它,你可以调用在 console.log(abc())


0
投票

你可以通过使用箭头功能进一步缩短它,如下图所示。

const abc = () => new Date().getHours()

console.log(abc());
© www.soinside.com 2019 - 2024. All rights reserved.