javascript:将秒转换为月 [英] javascript: convert seconds to months

查看:112
本文介绍了javascript:将秒转换为月的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am using the following script to do a countdown.

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个小时)

How can I show the months left too? (I want like: 3 months, 29 days and 3 hours)

谢谢。

推荐答案

使用这个简单的好函数。查看 实时演示

Use this simple nice function. Check out the 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:将秒转换为月的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆