根据Javascript中的日期更改文本

时间:2018-04-17 09:06:31

标签: javascript

我试图每周在第1周和第2周之间切换文字,具体取决于一系列日期。

目前它有效但仅限于指定的日期,我试图让它保留整周的文本,直到它到达下一个日期。

因此,如果日期是4月16日星期一,我希望它显示整周的第1周,直到4月23日星期一。

如果不是那样,那么每个星期日是否可以更改文本并保持相同的文本直到下一个星期日?

代码

var todaysdate = new Date();
var dd = todaysdate.getDate();
var mm = todaysdate.getMonth()+1; //January is 0!

var yyyy = todaysdate.getFullYear();
if(dd<10){
    dd='0'+dd;
} 
if(mm<10){
    mm='0'+mm;
} 
var todaysdate = dd+'/'+mm+'/'+yyyy;

var weekadates = 
["17/04/2018", "30/04/2018",//April
"14/05/2018",               //May
"04/06/2018", "18/06/2018",//June
"02/07/2018", "16/07/2018",//July
];

var weekbdates = 
["23/04/2018",             //April
"07/05/2018", "21/05/2018",//May
"11/06/2018", "25/06/2018",//June
"09/07/2018"               //July
];





var a = weekadates.indexOf(todaysdate);
var b = weekbdates.indexOf(todaysdate);


if (a > -1) 
{
    document.getElementById("changeText").innerHTML = "Week 1";
} 
else if (b > -1) 
{
    document.getElementById("changeText").innerHTML = "Week 2"; 
} else
{
    document.getElementById("changeText").innerHTML = "Holidays!";
}

https://jsfiddle.net/naLuny2x/2/

感谢您的帮助,非常感谢。

3 个答案:

答案 0 :(得分:0)

在尝试查找数组中的日期时,您不应该使用今天的日期,而是使用当周的星期一日期:

&#13;
&#13;
const monday = new Date();
monday.setDate(monday.getDate() - ((monday.getDay() + 6) % 7));
&#13;
&#13;
&#13;

然后在剩下的逻辑中使用它。

答案 1 :(得分:0)

使用自1970年1月1日00:00:00 UTC以来经过的毫秒数。

&#13;
&#13;
{
    function oddEvenWeek ( baseDate = "04/17/2018", current = Date.now() ) {
        let base = new Date( baseDate ).getTime();
        let now = new Date( current ).getTime();
        let fromBase = ( now - base ) / ( 1000 * 60 * 60 * 24 * 7 ); //Weeks from base date
        let weeks = fromBase % 2;
        if ( weeks % 1 >= 6 / 7 ) {
            console.log( "Holiday" );
        } else {
            if ( ( fromBase % 2 ) < 1 ) {
                console.log( "Week 1" );
            } else {
                console.log( "Week 2" );
            }
        }
    }

    oddEvenWeek("04/17/2018","04/17/2018");
    oddEvenWeek("04/17/2018","04/23/2018");
    oddEvenWeek("04/17/2018","04/24/2018");
    oddEvenWeek("04/17/2018","04/30/2018");
}
&#13;
&#13;
&#13;

答案 2 :(得分:0)

我找到了一个代码段来查找日历周here

/**
 * Returns the week number for this date.  dowOffset is the day of week the week
 * "starts" on for your locale - it can be from 0 to 6. If dowOffset is 1 (Monday),
 * the week returned is the ISO 8601 week number.
 * @param int dowOffset
 * @return int
 */
Date.prototype.getWeek = function (dowOffset) {
/*getWeek() was developed by Nick Baicoianu at MeanFreePath: http://www.meanfreepath.com */

    dowOffset = typeof(dowOffset) == 'int' ? dowOffset : 0; //default dowOffset to zero
    var newYear = new Date(this.getFullYear(),0,1);
    var day = newYear.getDay() - dowOffset; //the day of week the year begins on
    day = (day >= 0 ? day : day + 7);
    var daynum = Math.floor((this.getTime() - newYear.getTime() - 
    (this.getTimezoneOffset()-newYear.getTimezoneOffset())*60000)/86400000) + 1;
    var weeknum;
    //if the year starts before the middle of a week
    if(day < 4) {
        weeknum = Math.floor((daynum+day-1)/7) + 1;
        if(weeknum > 52) {
            nYear = new Date(this.getFullYear() + 1,0,1);
            nday = nYear.getDay() - dowOffset;
            nday = nday >= 0 ? nday : nday + 7;
            /*if the next year starts before the middle of
              the week, it is week #1 of that year*/
            weeknum = nday < 4 ? 1 : 53;
        }
    }
    else {
        weeknum = Math.floor((daynum+day-1)/7);
    }
    return weeknum;
};

然后,您可以使用模数()运算符进行计算,如果它是均匀或不均匀的一周:

var todaysdate = new Date();
var dd = todaysdate.getDate();
if(dd === 1) {
    document.getElementById("changeText").innerHTML = "Week " + (todaysdate.getWeek() % 2 + 1);
} else {
    document.getElementById("changeText").innerHTML = "Holidays!"
}