[英]compare date and time in JS
I am authenticating user using oAuth2 which gives me expiry token. 我正在使用oAuth2对用户进行身份验证,这给了我到期令牌。
The expiry token is for 3600 (which means one hour, as per oauth standard) 到期令牌是3600(这意味着一小时,根据oauth标准)
Now, when the token expire, I want to use refresh token to get new access token. 现在,当令牌过期时,我想使用刷新令牌来获取新的访问令牌。
To see if the token have expired, I am thinking about storing the value of current Date.now() and the value of Date.now() after one hour. 要查看令牌是否已过期,我正在考虑在一小时后存储当前Date.now()的值和Date.now()的值。
THe problem is that date.now()
gives value in long int ie 问题是
date.now()
在long int ie中给出了值
let date = Date.now() console.log(date)
Now, I am unable to comprehend how I can compare time here. 现在,我无法理解我如何在这里比较时间。 Can somenone help me.
有人可以帮助我。
use new Date()
intead of Date.now() 使用Date.now()的
new Date()
intead
If no arguments are provided, the constructor creates a JavaScript Date object for the current date and time according to system settings for timezone offset.
如果未提供参数,则构造函数将根据时区偏移的系统设置为当前日期和时间创建JavaScript Date对象。
then dates can be directly compared using the >
and <
operators 然后可以使用
>
和<
运算符直接比较日期
Here an example where compare the date: 这里是一个比较日期的例子:
Date.prototype.addHours= function(h){ //simulation after 1 hour this.setHours(this.getHours()+h); return this; } var dateOld = new Date(); var dateNow = new Date().addHours(1);//test after 1 hour //output: false console.log(dateOld.getTime() === dateNow.getTime());//compare the date
Hope this helps 希望这可以帮助
Since Date.now returns a time value and there are always exactly 3.6e6 ms in an ECMAScript hour, you can just add 3.6e6 to get the time value in one hour, eg 由于Date.now返回一个时间值,ECMAScript小时内总是精确到3.6e6毫秒,你可以添加3.6e6来获取一小时内的时间值,例如
var now = Date.now(); var expires = now + 3.6e6; console.log(`expired yet? ${now > expires}`); console.log(`now : ${new Date(now).toLocaleString()}\\nexpires: ${new Date(expires).toLocaleString()}`);
var time=new Date()
var now=String(time.getHours())+":"+String(time.getMinutes())+":"+String(time.getSeconds());
This will give you the time in H:M:S. 这将给你H:M:S的时间。 Format and this will be string.
格式,这将是字符串。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.