Change particles if is winter in javascript











up vote
2
down vote

favorite












I have two configurations (json) for particles js. One of them is for winter time, so it should be active from 21. December to 21. March. I tried many things, and still not working (Not because is still November :) )
Last thing I tried is this



var today = new Date();
var tDate = today.getDate();
var tMonth = today.getMonth();

var startingDate = new Date();
var sDate = startingDate.setDate(1);
var sMonth = startingDate.setMonth(10);

var endingDate = new Date();
var eDate = endingDate.setDate(21);
var eMonth = endingDate.setMonth(2);

if (tMonth >= sMonth && tMonth <= eMonth) {
if (tDate >= sDate && tDate <= eDate) {
particlesJS.load('particles', '../../Content/Scripts/particles/particles-config-winter.json');
}
}
else {
particlesJS.load('particles', '../../Content/Scripts/particles/particles-config.json');
}


This does not make sense, because starting and ending date is both 21. I tried to compare as string, didn't work out. I have no ideas how to compare two dates in javascript. Any help?










share|improve this question


















  • 1




    If you dont mind libraries to handle this for you, i would suggest you to look at momentJs which makes it easy to manipulate date and time.
    – Lagoni
    Nov 10 at 22:15






  • 1




    Wooow...I didn't even know that this exists. Can you put this as answer so I can mark it. Thank you very much!!!
    – mr. Panzerman
    Nov 10 at 22:19















up vote
2
down vote

favorite












I have two configurations (json) for particles js. One of them is for winter time, so it should be active from 21. December to 21. March. I tried many things, and still not working (Not because is still November :) )
Last thing I tried is this



var today = new Date();
var tDate = today.getDate();
var tMonth = today.getMonth();

var startingDate = new Date();
var sDate = startingDate.setDate(1);
var sMonth = startingDate.setMonth(10);

var endingDate = new Date();
var eDate = endingDate.setDate(21);
var eMonth = endingDate.setMonth(2);

if (tMonth >= sMonth && tMonth <= eMonth) {
if (tDate >= sDate && tDate <= eDate) {
particlesJS.load('particles', '../../Content/Scripts/particles/particles-config-winter.json');
}
}
else {
particlesJS.load('particles', '../../Content/Scripts/particles/particles-config.json');
}


This does not make sense, because starting and ending date is both 21. I tried to compare as string, didn't work out. I have no ideas how to compare two dates in javascript. Any help?










share|improve this question


















  • 1




    If you dont mind libraries to handle this for you, i would suggest you to look at momentJs which makes it easy to manipulate date and time.
    – Lagoni
    Nov 10 at 22:15






  • 1




    Wooow...I didn't even know that this exists. Can you put this as answer so I can mark it. Thank you very much!!!
    – mr. Panzerman
    Nov 10 at 22:19













up vote
2
down vote

favorite









up vote
2
down vote

favorite











I have two configurations (json) for particles js. One of them is for winter time, so it should be active from 21. December to 21. March. I tried many things, and still not working (Not because is still November :) )
Last thing I tried is this



var today = new Date();
var tDate = today.getDate();
var tMonth = today.getMonth();

var startingDate = new Date();
var sDate = startingDate.setDate(1);
var sMonth = startingDate.setMonth(10);

var endingDate = new Date();
var eDate = endingDate.setDate(21);
var eMonth = endingDate.setMonth(2);

if (tMonth >= sMonth && tMonth <= eMonth) {
if (tDate >= sDate && tDate <= eDate) {
particlesJS.load('particles', '../../Content/Scripts/particles/particles-config-winter.json');
}
}
else {
particlesJS.load('particles', '../../Content/Scripts/particles/particles-config.json');
}


This does not make sense, because starting and ending date is both 21. I tried to compare as string, didn't work out. I have no ideas how to compare two dates in javascript. Any help?










share|improve this question













I have two configurations (json) for particles js. One of them is for winter time, so it should be active from 21. December to 21. March. I tried many things, and still not working (Not because is still November :) )
Last thing I tried is this



var today = new Date();
var tDate = today.getDate();
var tMonth = today.getMonth();

var startingDate = new Date();
var sDate = startingDate.setDate(1);
var sMonth = startingDate.setMonth(10);

var endingDate = new Date();
var eDate = endingDate.setDate(21);
var eMonth = endingDate.setMonth(2);

if (tMonth >= sMonth && tMonth <= eMonth) {
if (tDate >= sDate && tDate <= eDate) {
particlesJS.load('particles', '../../Content/Scripts/particles/particles-config-winter.json');
}
}
else {
particlesJS.load('particles', '../../Content/Scripts/particles/particles-config.json');
}


This does not make sense, because starting and ending date is both 21. I tried to compare as string, didn't work out. I have no ideas how to compare two dates in javascript. Any help?







javascript date compare






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 10 at 22:11









mr. Panzerman

698




698








  • 1




    If you dont mind libraries to handle this for you, i would suggest you to look at momentJs which makes it easy to manipulate date and time.
    – Lagoni
    Nov 10 at 22:15






  • 1




    Wooow...I didn't even know that this exists. Can you put this as answer so I can mark it. Thank you very much!!!
    – mr. Panzerman
    Nov 10 at 22:19














  • 1




    If you dont mind libraries to handle this for you, i would suggest you to look at momentJs which makes it easy to manipulate date and time.
    – Lagoni
    Nov 10 at 22:15






  • 1




    Wooow...I didn't even know that this exists. Can you put this as answer so I can mark it. Thank you very much!!!
    – mr. Panzerman
    Nov 10 at 22:19








1




1




If you dont mind libraries to handle this for you, i would suggest you to look at momentJs which makes it easy to manipulate date and time.
– Lagoni
Nov 10 at 22:15




If you dont mind libraries to handle this for you, i would suggest you to look at momentJs which makes it easy to manipulate date and time.
– Lagoni
Nov 10 at 22:15




1




1




Wooow...I didn't even know that this exists. Can you put this as answer so I can mark it. Thank you very much!!!
– mr. Panzerman
Nov 10 at 22:19




Wooow...I didn't even know that this exists. Can you put this as answer so I can mark it. Thank you very much!!!
– mr. Panzerman
Nov 10 at 22:19












3 Answers
3






active

oldest

votes

















up vote
2
down vote



accepted










You could use this function:



function isWinter(dt) {
const m = dt.getMonth();
return m == 11 ? dt.getDate() >= 21 : m == 2 ? dt.getDate() < 21 : m < 2;
}


Call it for today's date like this:



if (isWinter(new Date())) {  /*....*/ }


One of the issues in your attempt is that setMonth and setDate methods return the modified date as number of milliseconds (not just a month or day part) and mutate the date on which the method is called.



Explanation of the function:



The ternary expression uses this logic:




  • If the month is December (11), then return true when the day-of-the-month is at least 21 or false otherwise: this is what dt.getDate() >= 21 evaluates to (true or false).

  • Otherwise, if the month is March (2), then return true when the day-of-the-month is less than 21 or false otherwise: same principle as above, but reversed

  • Otherwise, return true when the month comes before March ( < 2 ) and false otherwise: this is what m < 2 evaluates to.






share|improve this answer























  • And you, dear sir, you are God. Work like a charm...
    – mr. Panzerman
    Nov 10 at 23:23










  • I know it works, I tried...but can you explain to me how. You lost me with two ternary operators... I've never seen it before...I tried to break it down to if statement so I can understand, but no luck for me...
    – mr. Panzerman
    Nov 10 at 23:54






  • 1




    I added an explanation in the answer.
    – trincot
    Nov 11 at 0:00


















up vote
0
down vote













Date manipulation is always a struggle. If you don't mind libraries to handle this for you, i would suggest you to look at MomentJs which makes it easy to manipulate date and time. However each library you add, you add unnecessary code which you might not need. Therefore if you only need do a few date manipulations, it might not be worth it and trincot's's answer is perfect, if you don't really need MomentJs.



Following the line of trincot's answer, this is how you do the same thing in MomentJs:






function isWinter(date){
date.year(moment().year()); //Normalize year.
let winterStart = moment('21-12', 'DD-MM');
let winterEnd = moment('21-03', 'DD-MM');
return !date.isBetween(winterEnd, winterStart, 'day')
}
//Tests:
let today = moment('20-12', 'DD-MM');
alert(isWinter(today)); //false
today = moment('21-12', 'DD-MM');
alert(isWinter(today)); //true
today = moment('21-03', 'DD-MM');
alert(isWinter(today)); //true
today = moment('22-03', 'DD-MM');
alert(isWinter(today)); //false
today = moment('22-03-2019', 'DD-MM-YYYY');
alert(isWinter(today)); //false
today = moment('21-03-2019', 'DD-MM-YYYY');
alert(isWinter(today)); //true

<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.22.2/moment.min.js"></script>








share|improve this answer



















  • 1




    If today's date would be in January, then the ending date should not get its year increased, but the start date would need to get a year that is one less instead. NB: Winter does not start on 1 November ;-)
    – trincot
    Nov 10 at 23:52










  • We all know that winter begins on December 21 ... but I was too excited to check if this work, so I couldn't wait until then...that's why November... ;))
    – mr. Panzerman
    Nov 11 at 0:09










  • Yea, you right @trincot, will change it later :)
    – Lagoni
    Nov 11 at 0:12










  • A library recommendation is not an answer.
    – RobG
    Nov 11 at 11:28










  • @RobG you are right, that's why i suggested it in the comments on the post instead of creating it as an answer. Decided to do it any way, in case he wanted to go either way :) But i'll keep it in mind!
    – Lagoni
    Nov 11 at 11:33


















up vote
0
down vote













To explain why your code doesn't work:



var today = new Date();
var tDate = today.getDate();
var tMonth = today.getMonth();

var startingDate = new Date();

// This sets the date to the first of the month, and sets sDate to a number
// that is the time value of the adjusted date
var sDate = startingDate.setDate(1);

// This sets the month to November and sMonth to the adjusted time value
var sMonth = startingDate.setMonth(10);

var endingDate = new Date();

// As above, this sets endingDate to 21 March and sets eDate and eMonth to numbers
var eDate = endingDate.setDate(21);
var eMonth = endingDate.setMonth(2);

// This will never be true after 1970-01-01 as you're comparing a months to a time values
if (tMonth >= sMonth && tMonth <= eMonth) {
if (tDate >= sDate && tDate <= eDate) {


Even if this used correct values, the logic doesn't work as even for a date in the range, you're then comparing the date (day number) when that should only be compared if the month is March or November, not any other month (per Trincot's answer).



It's easier if you create dates for 21 March and 21 December in the current year, then see if the current date falls in the range. If it does, it's not winter. If it doesn't, it's winter. I've assumed that the range is inclusive, adjust the dates if that isn't the case.






function isWinter(date = new Date()) {
var start = new Date(date.getFullYear(), 2, 21);
var end = new Date(date.getFullYear(), 11, 21);
return date > start && date < end;
}

[new Date(2018, 0, 1), // 1 Jan 2018, winter
new Date(2018, 2,21), // 21 Mar 2018, winter
new Date(2018, 5, 1), // 1 Jun 2018, not winter
new Date(2018,11,21), // 21 Dec 2018, winter
new Date() // Today ...
].forEach(d => console.log(`${d} is ${isWinter(d)?'not ':''}winter`));








share|improve this answer





















    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














     

    draft saved


    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53243936%2fchange-particles-if-is-winter-in-javascript%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    3 Answers
    3






    active

    oldest

    votes








    3 Answers
    3






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    2
    down vote



    accepted










    You could use this function:



    function isWinter(dt) {
    const m = dt.getMonth();
    return m == 11 ? dt.getDate() >= 21 : m == 2 ? dt.getDate() < 21 : m < 2;
    }


    Call it for today's date like this:



    if (isWinter(new Date())) {  /*....*/ }


    One of the issues in your attempt is that setMonth and setDate methods return the modified date as number of milliseconds (not just a month or day part) and mutate the date on which the method is called.



    Explanation of the function:



    The ternary expression uses this logic:




    • If the month is December (11), then return true when the day-of-the-month is at least 21 or false otherwise: this is what dt.getDate() >= 21 evaluates to (true or false).

    • Otherwise, if the month is March (2), then return true when the day-of-the-month is less than 21 or false otherwise: same principle as above, but reversed

    • Otherwise, return true when the month comes before March ( < 2 ) and false otherwise: this is what m < 2 evaluates to.






    share|improve this answer























    • And you, dear sir, you are God. Work like a charm...
      – mr. Panzerman
      Nov 10 at 23:23










    • I know it works, I tried...but can you explain to me how. You lost me with two ternary operators... I've never seen it before...I tried to break it down to if statement so I can understand, but no luck for me...
      – mr. Panzerman
      Nov 10 at 23:54






    • 1




      I added an explanation in the answer.
      – trincot
      Nov 11 at 0:00















    up vote
    2
    down vote



    accepted










    You could use this function:



    function isWinter(dt) {
    const m = dt.getMonth();
    return m == 11 ? dt.getDate() >= 21 : m == 2 ? dt.getDate() < 21 : m < 2;
    }


    Call it for today's date like this:



    if (isWinter(new Date())) {  /*....*/ }


    One of the issues in your attempt is that setMonth and setDate methods return the modified date as number of milliseconds (not just a month or day part) and mutate the date on which the method is called.



    Explanation of the function:



    The ternary expression uses this logic:




    • If the month is December (11), then return true when the day-of-the-month is at least 21 or false otherwise: this is what dt.getDate() >= 21 evaluates to (true or false).

    • Otherwise, if the month is March (2), then return true when the day-of-the-month is less than 21 or false otherwise: same principle as above, but reversed

    • Otherwise, return true when the month comes before March ( < 2 ) and false otherwise: this is what m < 2 evaluates to.






    share|improve this answer























    • And you, dear sir, you are God. Work like a charm...
      – mr. Panzerman
      Nov 10 at 23:23










    • I know it works, I tried...but can you explain to me how. You lost me with two ternary operators... I've never seen it before...I tried to break it down to if statement so I can understand, but no luck for me...
      – mr. Panzerman
      Nov 10 at 23:54






    • 1




      I added an explanation in the answer.
      – trincot
      Nov 11 at 0:00













    up vote
    2
    down vote



    accepted







    up vote
    2
    down vote



    accepted






    You could use this function:



    function isWinter(dt) {
    const m = dt.getMonth();
    return m == 11 ? dt.getDate() >= 21 : m == 2 ? dt.getDate() < 21 : m < 2;
    }


    Call it for today's date like this:



    if (isWinter(new Date())) {  /*....*/ }


    One of the issues in your attempt is that setMonth and setDate methods return the modified date as number of milliseconds (not just a month or day part) and mutate the date on which the method is called.



    Explanation of the function:



    The ternary expression uses this logic:




    • If the month is December (11), then return true when the day-of-the-month is at least 21 or false otherwise: this is what dt.getDate() >= 21 evaluates to (true or false).

    • Otherwise, if the month is March (2), then return true when the day-of-the-month is less than 21 or false otherwise: same principle as above, but reversed

    • Otherwise, return true when the month comes before March ( < 2 ) and false otherwise: this is what m < 2 evaluates to.






    share|improve this answer














    You could use this function:



    function isWinter(dt) {
    const m = dt.getMonth();
    return m == 11 ? dt.getDate() >= 21 : m == 2 ? dt.getDate() < 21 : m < 2;
    }


    Call it for today's date like this:



    if (isWinter(new Date())) {  /*....*/ }


    One of the issues in your attempt is that setMonth and setDate methods return the modified date as number of milliseconds (not just a month or day part) and mutate the date on which the method is called.



    Explanation of the function:



    The ternary expression uses this logic:




    • If the month is December (11), then return true when the day-of-the-month is at least 21 or false otherwise: this is what dt.getDate() >= 21 evaluates to (true or false).

    • Otherwise, if the month is March (2), then return true when the day-of-the-month is less than 21 or false otherwise: same principle as above, but reversed

    • Otherwise, return true when the month comes before March ( < 2 ) and false otherwise: this is what m < 2 evaluates to.







    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited Nov 11 at 0:00

























    answered Nov 10 at 22:28









    trincot

    113k1477109




    113k1477109












    • And you, dear sir, you are God. Work like a charm...
      – mr. Panzerman
      Nov 10 at 23:23










    • I know it works, I tried...but can you explain to me how. You lost me with two ternary operators... I've never seen it before...I tried to break it down to if statement so I can understand, but no luck for me...
      – mr. Panzerman
      Nov 10 at 23:54






    • 1




      I added an explanation in the answer.
      – trincot
      Nov 11 at 0:00


















    • And you, dear sir, you are God. Work like a charm...
      – mr. Panzerman
      Nov 10 at 23:23










    • I know it works, I tried...but can you explain to me how. You lost me with two ternary operators... I've never seen it before...I tried to break it down to if statement so I can understand, but no luck for me...
      – mr. Panzerman
      Nov 10 at 23:54






    • 1




      I added an explanation in the answer.
      – trincot
      Nov 11 at 0:00
















    And you, dear sir, you are God. Work like a charm...
    – mr. Panzerman
    Nov 10 at 23:23




    And you, dear sir, you are God. Work like a charm...
    – mr. Panzerman
    Nov 10 at 23:23












    I know it works, I tried...but can you explain to me how. You lost me with two ternary operators... I've never seen it before...I tried to break it down to if statement so I can understand, but no luck for me...
    – mr. Panzerman
    Nov 10 at 23:54




    I know it works, I tried...but can you explain to me how. You lost me with two ternary operators... I've never seen it before...I tried to break it down to if statement so I can understand, but no luck for me...
    – mr. Panzerman
    Nov 10 at 23:54




    1




    1




    I added an explanation in the answer.
    – trincot
    Nov 11 at 0:00




    I added an explanation in the answer.
    – trincot
    Nov 11 at 0:00












    up vote
    0
    down vote













    Date manipulation is always a struggle. If you don't mind libraries to handle this for you, i would suggest you to look at MomentJs which makes it easy to manipulate date and time. However each library you add, you add unnecessary code which you might not need. Therefore if you only need do a few date manipulations, it might not be worth it and trincot's's answer is perfect, if you don't really need MomentJs.



    Following the line of trincot's answer, this is how you do the same thing in MomentJs:






    function isWinter(date){
    date.year(moment().year()); //Normalize year.
    let winterStart = moment('21-12', 'DD-MM');
    let winterEnd = moment('21-03', 'DD-MM');
    return !date.isBetween(winterEnd, winterStart, 'day')
    }
    //Tests:
    let today = moment('20-12', 'DD-MM');
    alert(isWinter(today)); //false
    today = moment('21-12', 'DD-MM');
    alert(isWinter(today)); //true
    today = moment('21-03', 'DD-MM');
    alert(isWinter(today)); //true
    today = moment('22-03', 'DD-MM');
    alert(isWinter(today)); //false
    today = moment('22-03-2019', 'DD-MM-YYYY');
    alert(isWinter(today)); //false
    today = moment('21-03-2019', 'DD-MM-YYYY');
    alert(isWinter(today)); //true

    <script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.22.2/moment.min.js"></script>








    share|improve this answer



















    • 1




      If today's date would be in January, then the ending date should not get its year increased, but the start date would need to get a year that is one less instead. NB: Winter does not start on 1 November ;-)
      – trincot
      Nov 10 at 23:52










    • We all know that winter begins on December 21 ... but I was too excited to check if this work, so I couldn't wait until then...that's why November... ;))
      – mr. Panzerman
      Nov 11 at 0:09










    • Yea, you right @trincot, will change it later :)
      – Lagoni
      Nov 11 at 0:12










    • A library recommendation is not an answer.
      – RobG
      Nov 11 at 11:28










    • @RobG you are right, that's why i suggested it in the comments on the post instead of creating it as an answer. Decided to do it any way, in case he wanted to go either way :) But i'll keep it in mind!
      – Lagoni
      Nov 11 at 11:33















    up vote
    0
    down vote













    Date manipulation is always a struggle. If you don't mind libraries to handle this for you, i would suggest you to look at MomentJs which makes it easy to manipulate date and time. However each library you add, you add unnecessary code which you might not need. Therefore if you only need do a few date manipulations, it might not be worth it and trincot's's answer is perfect, if you don't really need MomentJs.



    Following the line of trincot's answer, this is how you do the same thing in MomentJs:






    function isWinter(date){
    date.year(moment().year()); //Normalize year.
    let winterStart = moment('21-12', 'DD-MM');
    let winterEnd = moment('21-03', 'DD-MM');
    return !date.isBetween(winterEnd, winterStart, 'day')
    }
    //Tests:
    let today = moment('20-12', 'DD-MM');
    alert(isWinter(today)); //false
    today = moment('21-12', 'DD-MM');
    alert(isWinter(today)); //true
    today = moment('21-03', 'DD-MM');
    alert(isWinter(today)); //true
    today = moment('22-03', 'DD-MM');
    alert(isWinter(today)); //false
    today = moment('22-03-2019', 'DD-MM-YYYY');
    alert(isWinter(today)); //false
    today = moment('21-03-2019', 'DD-MM-YYYY');
    alert(isWinter(today)); //true

    <script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.22.2/moment.min.js"></script>








    share|improve this answer



















    • 1




      If today's date would be in January, then the ending date should not get its year increased, but the start date would need to get a year that is one less instead. NB: Winter does not start on 1 November ;-)
      – trincot
      Nov 10 at 23:52










    • We all know that winter begins on December 21 ... but I was too excited to check if this work, so I couldn't wait until then...that's why November... ;))
      – mr. Panzerman
      Nov 11 at 0:09










    • Yea, you right @trincot, will change it later :)
      – Lagoni
      Nov 11 at 0:12










    • A library recommendation is not an answer.
      – RobG
      Nov 11 at 11:28










    • @RobG you are right, that's why i suggested it in the comments on the post instead of creating it as an answer. Decided to do it any way, in case he wanted to go either way :) But i'll keep it in mind!
      – Lagoni
      Nov 11 at 11:33













    up vote
    0
    down vote










    up vote
    0
    down vote









    Date manipulation is always a struggle. If you don't mind libraries to handle this for you, i would suggest you to look at MomentJs which makes it easy to manipulate date and time. However each library you add, you add unnecessary code which you might not need. Therefore if you only need do a few date manipulations, it might not be worth it and trincot's's answer is perfect, if you don't really need MomentJs.



    Following the line of trincot's answer, this is how you do the same thing in MomentJs:






    function isWinter(date){
    date.year(moment().year()); //Normalize year.
    let winterStart = moment('21-12', 'DD-MM');
    let winterEnd = moment('21-03', 'DD-MM');
    return !date.isBetween(winterEnd, winterStart, 'day')
    }
    //Tests:
    let today = moment('20-12', 'DD-MM');
    alert(isWinter(today)); //false
    today = moment('21-12', 'DD-MM');
    alert(isWinter(today)); //true
    today = moment('21-03', 'DD-MM');
    alert(isWinter(today)); //true
    today = moment('22-03', 'DD-MM');
    alert(isWinter(today)); //false
    today = moment('22-03-2019', 'DD-MM-YYYY');
    alert(isWinter(today)); //false
    today = moment('21-03-2019', 'DD-MM-YYYY');
    alert(isWinter(today)); //true

    <script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.22.2/moment.min.js"></script>








    share|improve this answer














    Date manipulation is always a struggle. If you don't mind libraries to handle this for you, i would suggest you to look at MomentJs which makes it easy to manipulate date and time. However each library you add, you add unnecessary code which you might not need. Therefore if you only need do a few date manipulations, it might not be worth it and trincot's's answer is perfect, if you don't really need MomentJs.



    Following the line of trincot's answer, this is how you do the same thing in MomentJs:






    function isWinter(date){
    date.year(moment().year()); //Normalize year.
    let winterStart = moment('21-12', 'DD-MM');
    let winterEnd = moment('21-03', 'DD-MM');
    return !date.isBetween(winterEnd, winterStart, 'day')
    }
    //Tests:
    let today = moment('20-12', 'DD-MM');
    alert(isWinter(today)); //false
    today = moment('21-12', 'DD-MM');
    alert(isWinter(today)); //true
    today = moment('21-03', 'DD-MM');
    alert(isWinter(today)); //true
    today = moment('22-03', 'DD-MM');
    alert(isWinter(today)); //false
    today = moment('22-03-2019', 'DD-MM-YYYY');
    alert(isWinter(today)); //false
    today = moment('21-03-2019', 'DD-MM-YYYY');
    alert(isWinter(today)); //true

    <script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.22.2/moment.min.js"></script>








    function isWinter(date){
    date.year(moment().year()); //Normalize year.
    let winterStart = moment('21-12', 'DD-MM');
    let winterEnd = moment('21-03', 'DD-MM');
    return !date.isBetween(winterEnd, winterStart, 'day')
    }
    //Tests:
    let today = moment('20-12', 'DD-MM');
    alert(isWinter(today)); //false
    today = moment('21-12', 'DD-MM');
    alert(isWinter(today)); //true
    today = moment('21-03', 'DD-MM');
    alert(isWinter(today)); //true
    today = moment('22-03', 'DD-MM');
    alert(isWinter(today)); //false
    today = moment('22-03-2019', 'DD-MM-YYYY');
    alert(isWinter(today)); //false
    today = moment('21-03-2019', 'DD-MM-YYYY');
    alert(isWinter(today)); //true

    <script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.22.2/moment.min.js"></script>





    function isWinter(date){
    date.year(moment().year()); //Normalize year.
    let winterStart = moment('21-12', 'DD-MM');
    let winterEnd = moment('21-03', 'DD-MM');
    return !date.isBetween(winterEnd, winterStart, 'day')
    }
    //Tests:
    let today = moment('20-12', 'DD-MM');
    alert(isWinter(today)); //false
    today = moment('21-12', 'DD-MM');
    alert(isWinter(today)); //true
    today = moment('21-03', 'DD-MM');
    alert(isWinter(today)); //true
    today = moment('22-03', 'DD-MM');
    alert(isWinter(today)); //false
    today = moment('22-03-2019', 'DD-MM-YYYY');
    alert(isWinter(today)); //false
    today = moment('21-03-2019', 'DD-MM-YYYY');
    alert(isWinter(today)); //true

    <script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.22.2/moment.min.js"></script>






    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited Nov 11 at 2:30

























    answered Nov 10 at 22:39









    Lagoni

    15013




    15013








    • 1




      If today's date would be in January, then the ending date should not get its year increased, but the start date would need to get a year that is one less instead. NB: Winter does not start on 1 November ;-)
      – trincot
      Nov 10 at 23:52










    • We all know that winter begins on December 21 ... but I was too excited to check if this work, so I couldn't wait until then...that's why November... ;))
      – mr. Panzerman
      Nov 11 at 0:09










    • Yea, you right @trincot, will change it later :)
      – Lagoni
      Nov 11 at 0:12










    • A library recommendation is not an answer.
      – RobG
      Nov 11 at 11:28










    • @RobG you are right, that's why i suggested it in the comments on the post instead of creating it as an answer. Decided to do it any way, in case he wanted to go either way :) But i'll keep it in mind!
      – Lagoni
      Nov 11 at 11:33














    • 1




      If today's date would be in January, then the ending date should not get its year increased, but the start date would need to get a year that is one less instead. NB: Winter does not start on 1 November ;-)
      – trincot
      Nov 10 at 23:52










    • We all know that winter begins on December 21 ... but I was too excited to check if this work, so I couldn't wait until then...that's why November... ;))
      – mr. Panzerman
      Nov 11 at 0:09










    • Yea, you right @trincot, will change it later :)
      – Lagoni
      Nov 11 at 0:12










    • A library recommendation is not an answer.
      – RobG
      Nov 11 at 11:28










    • @RobG you are right, that's why i suggested it in the comments on the post instead of creating it as an answer. Decided to do it any way, in case he wanted to go either way :) But i'll keep it in mind!
      – Lagoni
      Nov 11 at 11:33








    1




    1




    If today's date would be in January, then the ending date should not get its year increased, but the start date would need to get a year that is one less instead. NB: Winter does not start on 1 November ;-)
    – trincot
    Nov 10 at 23:52




    If today's date would be in January, then the ending date should not get its year increased, but the start date would need to get a year that is one less instead. NB: Winter does not start on 1 November ;-)
    – trincot
    Nov 10 at 23:52












    We all know that winter begins on December 21 ... but I was too excited to check if this work, so I couldn't wait until then...that's why November... ;))
    – mr. Panzerman
    Nov 11 at 0:09




    We all know that winter begins on December 21 ... but I was too excited to check if this work, so I couldn't wait until then...that's why November... ;))
    – mr. Panzerman
    Nov 11 at 0:09












    Yea, you right @trincot, will change it later :)
    – Lagoni
    Nov 11 at 0:12




    Yea, you right @trincot, will change it later :)
    – Lagoni
    Nov 11 at 0:12












    A library recommendation is not an answer.
    – RobG
    Nov 11 at 11:28




    A library recommendation is not an answer.
    – RobG
    Nov 11 at 11:28












    @RobG you are right, that's why i suggested it in the comments on the post instead of creating it as an answer. Decided to do it any way, in case he wanted to go either way :) But i'll keep it in mind!
    – Lagoni
    Nov 11 at 11:33




    @RobG you are right, that's why i suggested it in the comments on the post instead of creating it as an answer. Decided to do it any way, in case he wanted to go either way :) But i'll keep it in mind!
    – Lagoni
    Nov 11 at 11:33










    up vote
    0
    down vote













    To explain why your code doesn't work:



    var today = new Date();
    var tDate = today.getDate();
    var tMonth = today.getMonth();

    var startingDate = new Date();

    // This sets the date to the first of the month, and sets sDate to a number
    // that is the time value of the adjusted date
    var sDate = startingDate.setDate(1);

    // This sets the month to November and sMonth to the adjusted time value
    var sMonth = startingDate.setMonth(10);

    var endingDate = new Date();

    // As above, this sets endingDate to 21 March and sets eDate and eMonth to numbers
    var eDate = endingDate.setDate(21);
    var eMonth = endingDate.setMonth(2);

    // This will never be true after 1970-01-01 as you're comparing a months to a time values
    if (tMonth >= sMonth && tMonth <= eMonth) {
    if (tDate >= sDate && tDate <= eDate) {


    Even if this used correct values, the logic doesn't work as even for a date in the range, you're then comparing the date (day number) when that should only be compared if the month is March or November, not any other month (per Trincot's answer).



    It's easier if you create dates for 21 March and 21 December in the current year, then see if the current date falls in the range. If it does, it's not winter. If it doesn't, it's winter. I've assumed that the range is inclusive, adjust the dates if that isn't the case.






    function isWinter(date = new Date()) {
    var start = new Date(date.getFullYear(), 2, 21);
    var end = new Date(date.getFullYear(), 11, 21);
    return date > start && date < end;
    }

    [new Date(2018, 0, 1), // 1 Jan 2018, winter
    new Date(2018, 2,21), // 21 Mar 2018, winter
    new Date(2018, 5, 1), // 1 Jun 2018, not winter
    new Date(2018,11,21), // 21 Dec 2018, winter
    new Date() // Today ...
    ].forEach(d => console.log(`${d} is ${isWinter(d)?'not ':''}winter`));








    share|improve this answer

























      up vote
      0
      down vote













      To explain why your code doesn't work:



      var today = new Date();
      var tDate = today.getDate();
      var tMonth = today.getMonth();

      var startingDate = new Date();

      // This sets the date to the first of the month, and sets sDate to a number
      // that is the time value of the adjusted date
      var sDate = startingDate.setDate(1);

      // This sets the month to November and sMonth to the adjusted time value
      var sMonth = startingDate.setMonth(10);

      var endingDate = new Date();

      // As above, this sets endingDate to 21 March and sets eDate and eMonth to numbers
      var eDate = endingDate.setDate(21);
      var eMonth = endingDate.setMonth(2);

      // This will never be true after 1970-01-01 as you're comparing a months to a time values
      if (tMonth >= sMonth && tMonth <= eMonth) {
      if (tDate >= sDate && tDate <= eDate) {


      Even if this used correct values, the logic doesn't work as even for a date in the range, you're then comparing the date (day number) when that should only be compared if the month is March or November, not any other month (per Trincot's answer).



      It's easier if you create dates for 21 March and 21 December in the current year, then see if the current date falls in the range. If it does, it's not winter. If it doesn't, it's winter. I've assumed that the range is inclusive, adjust the dates if that isn't the case.






      function isWinter(date = new Date()) {
      var start = new Date(date.getFullYear(), 2, 21);
      var end = new Date(date.getFullYear(), 11, 21);
      return date > start && date < end;
      }

      [new Date(2018, 0, 1), // 1 Jan 2018, winter
      new Date(2018, 2,21), // 21 Mar 2018, winter
      new Date(2018, 5, 1), // 1 Jun 2018, not winter
      new Date(2018,11,21), // 21 Dec 2018, winter
      new Date() // Today ...
      ].forEach(d => console.log(`${d} is ${isWinter(d)?'not ':''}winter`));








      share|improve this answer























        up vote
        0
        down vote










        up vote
        0
        down vote









        To explain why your code doesn't work:



        var today = new Date();
        var tDate = today.getDate();
        var tMonth = today.getMonth();

        var startingDate = new Date();

        // This sets the date to the first of the month, and sets sDate to a number
        // that is the time value of the adjusted date
        var sDate = startingDate.setDate(1);

        // This sets the month to November and sMonth to the adjusted time value
        var sMonth = startingDate.setMonth(10);

        var endingDate = new Date();

        // As above, this sets endingDate to 21 March and sets eDate and eMonth to numbers
        var eDate = endingDate.setDate(21);
        var eMonth = endingDate.setMonth(2);

        // This will never be true after 1970-01-01 as you're comparing a months to a time values
        if (tMonth >= sMonth && tMonth <= eMonth) {
        if (tDate >= sDate && tDate <= eDate) {


        Even if this used correct values, the logic doesn't work as even for a date in the range, you're then comparing the date (day number) when that should only be compared if the month is March or November, not any other month (per Trincot's answer).



        It's easier if you create dates for 21 March and 21 December in the current year, then see if the current date falls in the range. If it does, it's not winter. If it doesn't, it's winter. I've assumed that the range is inclusive, adjust the dates if that isn't the case.






        function isWinter(date = new Date()) {
        var start = new Date(date.getFullYear(), 2, 21);
        var end = new Date(date.getFullYear(), 11, 21);
        return date > start && date < end;
        }

        [new Date(2018, 0, 1), // 1 Jan 2018, winter
        new Date(2018, 2,21), // 21 Mar 2018, winter
        new Date(2018, 5, 1), // 1 Jun 2018, not winter
        new Date(2018,11,21), // 21 Dec 2018, winter
        new Date() // Today ...
        ].forEach(d => console.log(`${d} is ${isWinter(d)?'not ':''}winter`));








        share|improve this answer












        To explain why your code doesn't work:



        var today = new Date();
        var tDate = today.getDate();
        var tMonth = today.getMonth();

        var startingDate = new Date();

        // This sets the date to the first of the month, and sets sDate to a number
        // that is the time value of the adjusted date
        var sDate = startingDate.setDate(1);

        // This sets the month to November and sMonth to the adjusted time value
        var sMonth = startingDate.setMonth(10);

        var endingDate = new Date();

        // As above, this sets endingDate to 21 March and sets eDate and eMonth to numbers
        var eDate = endingDate.setDate(21);
        var eMonth = endingDate.setMonth(2);

        // This will never be true after 1970-01-01 as you're comparing a months to a time values
        if (tMonth >= sMonth && tMonth <= eMonth) {
        if (tDate >= sDate && tDate <= eDate) {


        Even if this used correct values, the logic doesn't work as even for a date in the range, you're then comparing the date (day number) when that should only be compared if the month is March or November, not any other month (per Trincot's answer).



        It's easier if you create dates for 21 March and 21 December in the current year, then see if the current date falls in the range. If it does, it's not winter. If it doesn't, it's winter. I've assumed that the range is inclusive, adjust the dates if that isn't the case.






        function isWinter(date = new Date()) {
        var start = new Date(date.getFullYear(), 2, 21);
        var end = new Date(date.getFullYear(), 11, 21);
        return date > start && date < end;
        }

        [new Date(2018, 0, 1), // 1 Jan 2018, winter
        new Date(2018, 2,21), // 21 Mar 2018, winter
        new Date(2018, 5, 1), // 1 Jun 2018, not winter
        new Date(2018,11,21), // 21 Dec 2018, winter
        new Date() // Today ...
        ].forEach(d => console.log(`${d} is ${isWinter(d)?'not ':''}winter`));








        function isWinter(date = new Date()) {
        var start = new Date(date.getFullYear(), 2, 21);
        var end = new Date(date.getFullYear(), 11, 21);
        return date > start && date < end;
        }

        [new Date(2018, 0, 1), // 1 Jan 2018, winter
        new Date(2018, 2,21), // 21 Mar 2018, winter
        new Date(2018, 5, 1), // 1 Jun 2018, not winter
        new Date(2018,11,21), // 21 Dec 2018, winter
        new Date() // Today ...
        ].forEach(d => console.log(`${d} is ${isWinter(d)?'not ':''}winter`));





        function isWinter(date = new Date()) {
        var start = new Date(date.getFullYear(), 2, 21);
        var end = new Date(date.getFullYear(), 11, 21);
        return date > start && date < end;
        }

        [new Date(2018, 0, 1), // 1 Jan 2018, winter
        new Date(2018, 2,21), // 21 Mar 2018, winter
        new Date(2018, 5, 1), // 1 Jun 2018, not winter
        new Date(2018,11,21), // 21 Dec 2018, winter
        new Date() // Today ...
        ].forEach(d => console.log(`${d} is ${isWinter(d)?'not ':''}winter`));






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 11 at 12:04









        RobG

        96k19101143




        96k19101143






























             

            draft saved


            draft discarded



















































             


            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53243936%2fchange-particles-if-is-winter-in-javascript%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Florida Star v. B. J. F.

            Danny Elfman

            Lugert, Oklahoma