当前位置:网站首页>JS - set countdown for Date object case
JS - set countdown for Date object case
2022-06-13 08:22:00 【tingkeaiii】
One 、 subject
Set the countdown
Two 、 Source code and effect
<script>
// The countdown effect
function getT(t) {
var time2 = +new Date(t); /* Set the timestamp of the time */
var extra = time2 - Date.now(); /* Timestamp difference */
var h = extra * 3 * Math.pow(10, -7);/* Convert to hours */
var mi = 60 * (h - Math.floor(h)); /* The decimal point of an hour is converted to a minute */
var s = 60 * (mi - Math.floor(mi)); /* The decimal point of a minute is converted to a second */
h = h < 10 ? '0' + Math.floor(h) : Math.floor(h);/* Round all down */
mi = mi < 10 ? '0' + Math.floor(mi) : Math.floor(mi);
s = s < 10 ? '0' + Math.floor(s) : Math.floor(s);
return h + ':' + mi + ':' + s;
}
var result = getT('2021-12-3 17:30:0');
console.log(result);
</script>
3、 ... and 、 Ideas
This question does not use the setting time minus the current time to get the countdown , It is not convenient to subtract . Instead, time stamps are used ( This makes it easy to subtract ), Subtract the timestamp of the current time from the timestamp of the set time . The obtained millisecond difference is converted into minutes and seconds .
- use +new Date( Time string ) Get the timestamp of the set time
The time string is brought in as an argument , The total number of milliseconds of the set time is obtained .
2. Subtract the total milliseconds of the current time from the total milliseconds of the set time , Get the error free millisecond difference .
3. Convert from this table , Convert the resulting millisecond difference into hours .pow( ) You can do power operations .
4. Take out the whole number of hours , Subtract to get the following decimal places , The decimal places are converted into minutes , The decimal places of minutes are converted into seconds …
5. Because all the hours, minutes and seconds are floating-point numbers , Then take an integer down , And automatic supplement 0, format , Can be more beautiful
边栏推荐
- Motiko basic syntax in dfinity (ICP) -8
- Young's matrix to find whether a number exists
- Altium Designer中导入和导出设置的方法
- 酒水批发行业应当如何高效管理商品与库存
- PHP isset() method ignores data error handling caused by null parameter value
- Microservice project construction III: automatic code generation
- Disk C is full? A few simple tips teach you to release and clean up tens of gigabytes of space on the C disk, the most effective way to clean up the C disk
- Which is the stronger fresh food distribution and sorting management system?
- CCNP_ Bt- Reissue
- [virt-manager]远程管理启动虚拟机时,鼠标飘逸的问题
猜你喜欢
Remote access and control
HCIP_ MGRE comprehensive experiment
将solidworks建的机器人模型导入到ros中
适合生鲜批发行业的几种精准接单方式
How to efficiently manage commodities and inventory in the beverage wholesale industry
第115页的gtk+编程例子——最简单的进度条2附带使用Anjuta写gtk程序的步骤
Penetration problem (main directory, password explosion, database uploading Trojan horse)
Win10系统如何修改桌面路径
MySQL installation and configuration under Windows
ERP基础数据 金蝶
随机推荐
Is signed or unsigned selected to create an integer field in MySQL? The answer is as follows:
将solidworks建的机器人模型导入到ros中
Disk C is full? A few simple tips teach you to release and clean up tens of gigabytes of space on the C disk, the most effective way to clean up the C disk
Set implements list search and exclusion
IPFs cluster setup process
How to use annotations in word
Dest0g3 520 orientation
Bidirectional retransmission step experiment
ERP basic data Kingdee
中小型照明灯饰行业如何利用数字化转型突出重围?
批发商为什么要使用订单系统
学习记录4: einops // cudnn.benchamark=true // hook
微服务系统架构搭建一:环境搭建
Dfinity (ICP) development problems and solutions-6
水仙花升级版(自幂数)
Young's matrix to find whether a number exists
Format_ String_ Server
CCNP_ Bt- Reissue
[game theory complete information static game] Nash equilibrium
MySQL query exercise 3