如何显示区块链合约调用的时间?
How to show the time when the blockchain contract called?
我想显示区块链合约调用的时间
我目前正在使用这样的方法在区块链中节省时间
function userCheckIn(uint placeid) public {
userCount++;
checkins[userCount] = Checkin(placeid, msg.sender, now);
}
然而,now
在前端显示随机数是这样的
1555650125
1555651118
请问有什么建议吗?
在此先感谢您。
这些不是随机数。这些是时间戳,它们代表自 1970 年 1 月 1 日以来经过的毫秒数。为了提取日期,您需要这样做:
function userCheckIn(uint placeid) public {
userCount++;
checkins[userCount] = Checkin(placeid, msg.sender, new Date(now) );
}
因为 now
在前端给你有效的时间戳,new Date(now)
会很容易给你时间和日期。如果您想按月、日、小时等进一步细化此日期,而不是使用默认的 js 方法,您可以查找 momentJS library.
时间戳似乎是对的。在大多数编程语言和计算机系统中,时间都被存储为时间戳,时间戳以纪元(unix timestamp)存储。这些是大(长)数,表示从某个指定的预定义时间开始的秒数。
要将此纪元时间戳转换为人类可读时间,您可以使用任何在其构造函数中采用纪元时间戳的库。
// Create a new JavaScript Date object based on the timestamp
// multiplied by 1000 so that the argument is in milliseconds, not seconds.
var date = new Date(unix_timestamp*1000);
// Hours part from the timestamp
var hours = date.getHours();
// Minutes part from the timestamp
var minutes = "0" + date.getMinutes();
// Seconds part from the timestamp
var seconds = "0" + date.getSeconds();
// Will display time in 10:30:23 format
var formattedTime = hours + ':' + minutes.substr(-2) + ':' + seconds.substr(-2);
有关详细信息,请参阅此 post。
我想显示区块链合约调用的时间
我目前正在使用这样的方法在区块链中节省时间
function userCheckIn(uint placeid) public {
userCount++;
checkins[userCount] = Checkin(placeid, msg.sender, now);
}
然而,now
在前端显示随机数是这样的
1555650125
1555651118
请问有什么建议吗?
在此先感谢您。
这些不是随机数。这些是时间戳,它们代表自 1970 年 1 月 1 日以来经过的毫秒数。为了提取日期,您需要这样做:
function userCheckIn(uint placeid) public {
userCount++;
checkins[userCount] = Checkin(placeid, msg.sender, new Date(now) );
}
因为 now
在前端给你有效的时间戳,new Date(now)
会很容易给你时间和日期。如果您想按月、日、小时等进一步细化此日期,而不是使用默认的 js 方法,您可以查找 momentJS library.
时间戳似乎是对的。在大多数编程语言和计算机系统中,时间都被存储为时间戳,时间戳以纪元(unix timestamp)存储。这些是大(长)数,表示从某个指定的预定义时间开始的秒数。
要将此纪元时间戳转换为人类可读时间,您可以使用任何在其构造函数中采用纪元时间戳的库。
// Create a new JavaScript Date object based on the timestamp
// multiplied by 1000 so that the argument is in milliseconds, not seconds.
var date = new Date(unix_timestamp*1000);
// Hours part from the timestamp
var hours = date.getHours();
// Minutes part from the timestamp
var minutes = "0" + date.getMinutes();
// Seconds part from the timestamp
var seconds = "0" + date.getSeconds();
// Will display time in 10:30:23 format
var formattedTime = hours + ':' + minutes.substr(-2) + ':' + seconds.substr(-2);
有关详细信息,请参阅此 post。