【问题标题】:Countdown to a single day each year (and find the date of that day in current year)倒计时到每年的某一天(并找到当年的那一天的日期)
【发布时间】:2017-09-06 10:45:58
【问题描述】:

我想做一些相当复杂的事情,我一直在使用 moment.js 或 countdown.js 来尝试解决这个问题,但我认为我的要求太复杂了?我可能错了。这是标准...

我需要能够实现以下目标,而不必每年手动更改日期,只需添加一次并在一页上有许多倒计时。

  1. 查找当前日期
  2. 查找当前年份
  3. 查找当前月份
  4. 在一个月的一周内查找适用的日期 ¬ 第 3 个星期日或第 2 个星期六
  5. 转成JS输出为html并运行倒计时
  6. 当日期过去时 - 重置为下一年

相当精神。例如,如果一个事件总是在三月的第三个星期日。日期不会每年都一样。

  1. 2016 年 - 3 月 19 日星期日
  2. 2017 - 3 月 20 日星期日
  3. 2018 - 3 月 18 日星期日等

我希望这能得到很好的解释,但我意识到这可能是一团糟。我设法通过手动添加的日期每年重置它,但后来有人扔了 date 的扳手每年都不同。

var event = new Date();
event = new Date(event.getFullYear() + 1, 3 - 1, 19);
jQuery('#dateEvent').countdown({ until: event });


<div id="dateEvent"></div>

【问题讨论】:

  • 我建议您使用moment.js 来获得正确的一天。然后你可以使用这里给出的答案:stackoverflow.com/questions/33335554/…
  • 嗨 - 好吧,看起来很有希望,但我想我必须为每个事件设置函数?会有数百个。我也在试图弄清楚如何输出这一刻。我可以使用我设置的另一个计时器 var time = moment().format('h:mm:ss a');然后 $('.time').text(time);
  • 您只需要一年、一个月以及一个月中给定日期的哪个间隔,其余的您可以自动化。如果用户输入一个日期,或者从数据库中提供一个日期,你可以计算它是哪一天,它是在月份中出现的,并用它创建你的 moment.js 对象。唯一不同的是第一次发生此类事件的“开始日期”。
  • 您应该“自行回答”您的问题,这样其他访问者才能看到答案,并且在 2 天内您可以接受该答案作为解决方案。您不应该在问题中发布答案。
  • 会做@Tschallacka

标签: javascript jquery datetime momentjs countdowntimer


【解决方案1】:

我已经编辑了这个答案,因为我现在整理了一个适合我的解决方案。因为我相信这不是简单的编码,因为它实际上并没有回答“请,这是基本编码”。拿起一本 JavaScript 书,学习编码',是的,谢谢...

// get the specific day of the week in the month in the year
        function getDay(month) {
            // Convert date to moment (month 0-11)
            var myMonth = moment("April", "MMMM");
            // Get first Sunday of the first week of the month
            var getDay = myMonth.weekday(0); // sunday is 0
            var nWeeks = 3; // 0 is 1st week
            // Check if first Sunday is in the given month
            if (getDay.month() != month) {
                nWeeks++;
            }
            // Return 3rd Sunday of the month formatted (custom format)
            return getDay.add(nWeeks, 'weeks').format("Y-MM-D h:mm:ss");
        }

        // print out the date as HTML and wrap in span
        document.getElementById("day").innerHTML = '<span>' + getDay() + '</span>';

使用

<script src="moment.js"></script>

希望它对某人有所帮助 - 当我确定如何在检查当前日期和事件已过去 1 年后,我会更新。我去看看那本 JS 书。

【讨论】:

    【解决方案2】:

    请看下面的代码,我在评论中解释了什么。

    您可以通过提供任何希望的开始日期的 javascript Date 对象来使用它,然后添加您希望知道日期的相应年份作为第二个值。

    var date = new Date("2016-03-20");
    
    function getDayInYear(startDate, year) {
        // get a moment instance of the start date
        var start = moment(startDate);
        // collect the moment.js values for the day and month
        var day = start.day();
        var month = start.month();
        // calculate which week in the month the date is. 
        var nthWeekOfMoth = Math.ceil(start.date() / 7);
        // Build up the new moment with a date object, passing the requested year, month and week in it
        var newMoment = moment(new Date(year,month,(nthWeekOfMoth * 7)));
        // Return the next instance of the requested day from the current newMoment date value.
        return newMoment.day(day);
    }
    
    var oldMoment = moment(date);
    var newMoment2017 = getDayInYear(date,2017);
    var newMoment2018 = getDayInYear(date,2018);
    
    console.log(oldMoment.format('YYYY MMMM dddd DD'));
    console.log(newMoment2017.format('YYYY MMMM dddd DD'));
    console.log(newMoment2018.format('YYYY MMMM dddd DD'));
    
    /** working from today up to 10 years into the future **/
    
    var date = new Date();
    var year = date.getFullYear();
    for(var i = 0; i < 11; i++) {
        console.log(getDayInYear(date, year+i).format('YYYY MMMM dddd DD'));
    }
    &lt;script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.18.1/moment.js"&gt;&lt;/script&gt;

    【讨论】:

    • 好的,这可以更好地解释正在发生的事情。它仍然需要手动输入日期,但我需要避免。我需要它知道当前日期并从那以后逐年向前推进。我将不得不更多地查看 moment.js 文档,看看这是否可行。添加 document.getElementById("test").innerHTML = (date);它只会显示 2016 年等,现在需要成为 2017 年的日期。
    • 当前日期只是var date = new Date()
    • 嗨@Tschallacka 是的,确实得到了当前日期。我需要将其合并到上述原始阶段 1 - 6 的功能中。到目前为止,您已经帮助获得了一年中的某一天,这很棒,但我需要让它从每年开始自动找到它,以今天为例。将其作为倒数计时器抽到下一年,然后重新设置。我想我还有很长的路要走。我不想手动输入除原始第一个日期之外的任何日期作为脚本的参考,以了解倒计时的日期。
    • 查看我的编辑,我刚刚编辑了一个输出今年和未来 10 年日期的示例。请,这是基本编码。拿起一本 JavaScript 书,学习编码。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-02-07
    • 1970-01-01
    • 2016-01-22
    • 2014-08-03
    • 2023-04-02
    • 1970-01-01
    • 2021-02-06
    相关资源
    最近更新 更多