gpt4 book ai didi

javascript:将秒转换为月

转载 作者:行者123 更新时间:2023-11-30 18:02:43 25 4
gpt4 key购买 nike

我正在使用以下脚本进行倒计时。

function startCountdown() {
var countdownDate = new Date("9/13/2013 12:00 AM");
var currentDate = new Date();
var dateDifference = new Date(countdownDate - currentDate);
var seconds = dateDifference.valueOf() / 1000;
CountBack(seconds);
}

function CountBack(seconds) {
var DisplayFormat = "DAYS days and HOURS hours";
DisplayFormat = DisplayFormat.replace("DAYS", (Math.floor(seconds/86400)) % 100000);
DisplayFormat = DisplayFormat.replace("HOURS", (Math.floor(seconds/3600)) % 24);
$('span.countdown').html(DisplayFormat);
}

startCountdown();

我怎样才能显示剩余的月份? (我想要:3 个月 29 天 3 小时)

谢谢。

最佳答案

使用这个简单的好函数。查看 LIVE DEMO :

var cc = new Date("9/13/2012 12:00 AM"),
c = new Date();

function timeDifference(d, dd) {
var hour = 60 * 60 * 1000,
day = hour * 24,
month = day * 30,
ms = Math.abs(d - dd),
months = parseInt(ms / month, 10);

ms = ms - months * month;
var days = parseInt(ms / day, 10);
ms -= days * day;
var hours = parseInt(ms / hour, 10);
ms -= hours * hour;

return [
months + " months",
days + " days",
hours + " hours"
].join(", ");
};

document.body.innerHTML += timeDifference(cc, c) + "<br />";

关于javascript:将秒转换为月,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16530474/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com