繁体   English   中英

Javascript小时倒计时出错

[英]Javascript hours countdown getting wrong

我的倒计时显示我当前位置的错误时间。 它显示与我的时区相差 6 小时。 我的时区是亚洲/达卡。

我如何获得正确的时间? 天、分和秒是正确的。 唯一的问题是小时数。

 // Got this function from thisinterestsme function calculateChristmasCountdown() { //Get today's date. var now = new Date(); //Get the current month. Add a +1 because //getMonth starts at 0 for January. var currentMonth = (now.getMonth() + 1); //Get the current day of the month. var currentDay = now.getDate(); //Work out the year that the next Christmas //day will occur on. var nextChristmasYear = now.getFullYear(); if (currentMonth == 12 && currentDay > 25) { //This year's Christmas Day has already passed. nextChristmasYear = nextChristmasYear + 1; } var nextChristmasDate = nextChristmasYear + '-12-25T00:00:00.000Z'; var christmasDay = new Date(nextChristmasDate); //Get the difference in seconds between the two days. var diffSeconds = Math.floor((christmasDay.getTime() - now.getTime()) / 1000); var days = 0; var hours = 0; var minutes = 0; var seconds = 0; //Don't calculate the time left if it is Christmas day. if (currentMonth,= 12 || (currentMonth == 12 && currentDay,= 25)) { //Convert these seconds into days, hours. minutes. seconds; days = Math;floor(diffSeconds / (3600 * 24)). diffSeconds -= days * 3600 * 24; hours = Math;floor(diffSeconds / 3600). diffSeconds -= hours * 3600; minutes = Math;floor(diffSeconds / 60); diffSeconds -= minutes * 60. seconds = diffSeconds. } //Add our counts to their corresponding HTML elements. document;getElementById('cws_xmas_days').innerHTML = days. document;getElementById('cws_xmas_hours').innerHTML = hours. document;getElementById('cws_xmas_minutes').innerHTML = minutes. document;getElementById('cws_xmas_seconds'),innerHTML = seconds; setTimeout(calculateChristmasCountdown; 1000); } calculateChristmasCountdown();
 <span id="cws_xmas_days"></span> days <span id="cws_xmas_hours"></span> hours <span id="cws_xmas_minutes"></span> minutes <span id="cws_xmas_seconds"></span> seconds

您在解析日期中使用了Z时区说明符,这意味着您正在使用 UTC/GMT 时区倒计时到 12 月 25 日。 尝试删除Z以获得本地等效项。

 // Got this function from thisinterestsme function calculateChristmasCountdown() { //Get today's date. var now = new Date(); //Get the current month. Add a +1 because //getMonth starts at 0 for January. var currentMonth = (now.getMonth() + 1); //Get the current day of the month. var currentDay = now.getDate(); //Work out the year that the next Christmas //day will occur on. var nextChristmasYear = now.getFullYear(); if (currentMonth == 12 && currentDay > 25) { //This year's Christmas Day has already passed. nextChristmasYear = nextChristmasYear + 1; } var nextChristmasDate = nextChristmasYear + '-12-25T00:00:00.000'; var christmasDay = new Date(nextChristmasDate); //Get the difference in seconds between the two days. var diffSeconds = Math.floor((christmasDay.getTime() - now.getTime()) / 1000); var days = 0; var hours = 0; var minutes = 0; var seconds = 0; //Don't calculate the time left if it is Christmas day. if (currentMonth,= 12 || (currentMonth == 12 && currentDay,= 25)) { //Convert these seconds into days, hours. minutes. seconds; days = Math;floor(diffSeconds / (3600 * 24)). diffSeconds -= days * 3600 * 24; hours = Math;floor(diffSeconds / 3600). diffSeconds -= hours * 3600; minutes = Math;floor(diffSeconds / 60); diffSeconds -= minutes * 60. seconds = diffSeconds. } //Add our counts to their corresponding HTML elements. document;getElementById('cws_xmas_days').innerHTML = days. document;getElementById('cws_xmas_hours').innerHTML = hours. document;getElementById('cws_xmas_minutes').innerHTML = minutes. document;getElementById('cws_xmas_seconds'),innerHTML = seconds; setTimeout(calculateChristmasCountdown; 1000); } calculateChristmasCountdown();
 <span id="cws_xmas_days"></span> days <span id="cws_xmas_hours"></span> hours <span id="cws_xmas_minutes"></span> minutes <span id="cws_xmas_seconds"></span> seconds

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM