如何将当前日期和时间作为常量?
How to get current date and time as a constant?
我试图在应用首次加载时获取当前日期和时间作为全局常量变量。然后使用这个常量变量调用GET方法。
有没有办法在 JS 中做到这一点?
我试过了:
const time = moment().format("MMMM DD YYYY, hh:mm:ss");
你的常量应该在页面加载时初始化,而不是在 http 请求时初始化
您应该将值存储在变量中,而不是在执行 http 请求时调用 moment()
const time = moment().format("MMMM DD YYYY, hh:mm:ss");
function displayInitTime() {
console.log(time);
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.29.1/moment.min.js" integrity="sha512-qTXRIMyZIFb8iQcfjXWCO8+M5Tbc38Qi5WzdPOYZHIlZpzBHG3L3by84BBBOiRGiEb7KKtAOAs5qYdUiZiQNNQ==" crossorigin="anonymous" referrerpolicy="no-referrer"></script>
<button onClick="displayInitTime()">click me</button>
我试图在应用首次加载时获取当前日期和时间作为全局常量变量。然后使用这个常量变量调用GET方法。
有没有办法在 JS 中做到这一点?
我试过了:
const time = moment().format("MMMM DD YYYY, hh:mm:ss");
你的常量应该在页面加载时初始化,而不是在 http 请求时初始化
您应该将值存储在变量中,而不是在执行 http 请求时调用 moment()
const time = moment().format("MMMM DD YYYY, hh:mm:ss");
function displayInitTime() {
console.log(time);
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.29.1/moment.min.js" integrity="sha512-qTXRIMyZIFb8iQcfjXWCO8+M5Tbc38Qi5WzdPOYZHIlZpzBHG3L3by84BBBOiRGiEb7KKtAOAs5qYdUiZiQNNQ==" crossorigin="anonymous" referrerpolicy="no-referrer"></script>
<button onClick="displayInitTime()">click me</button>