2

I have a start date and end date from my request need to find whether the given day is weekday or weekend or holiday(from list of holidays in db)

I tried using moment.js

Mehravish Temkar
  • 4,275
  • 3
  • 25
  • 44
deepak masakale
  • 105
  • 1
  • 8
  • Figuring out if a date is a weekend or not and figuring out if it is a holiday are two different problems. You have to show a [mcve] that shows what you have tried and you need to explain what exact problem you have. – t.niese Apr 15 '19 at 04:54

2 Answers2

0

moment.js is just a library to manage dates, what you need is an algorithm to find if one of the dates is weekend or weekday. So what you can do is get the day number from the date (0-6) where 0 is sunday.

Let's say

var date = moment("2015-07-02");
var dow = date.day();

And about holidays or not or not, that's simple also, you need to get an array with all the dates in the same format that you are using with moment.js and iterate through the array to do it something like:

var holidays = ["2019-07-02", "2019-07-04"]

let isHoliday = holidays.find(x => x === "2015-07-02")

You need to do some work after all you need is here.

Merlyn007
  • 434
  • 1
  • 8
  • 21
0

To determine if a given date is a weekend is actually pretty simple and already has an answer here.

To determine if a given date is holiday is bit more complex, you can not achieve it with native JavaScript, you will need the help of external package.

I recommend you to use moment-holiday, it has a simplest API:

moment('2017-12-25').isHoliday();
//Christmas Day

moment('2005-03-15').isHoliday();
//false

moment('2009-10-31').isHoliday('Halloween');
//true

moment('2017-12-31').isHoliday();
//New Year's Eve

moment('2017-12-31').isHoliday(null, true);
//false

Or you can use date-holidays.

Shahar Shokrani
  • 7,598
  • 9
  • 48
  • 91
  • "*…and already has an answer*". Then you should mark this question as a duplicate, not answer it again. "… you can not achieve it with native JavaScript,*" is incorrect, *moment.js* **is** native javascript. It also requires locale files for the required localities, there are many holidays observed in some places but not others. Even within a particular locality, some some holidays are observed by some citizens but not others (e.g. bank and school holidays, certain religious holidays). – RobG Apr 15 '19 at 07:46
  • PS. For me, 2017-12-31 is a holiday because if parsed as UTC (per ECMA-262) it's 2018-01-01 where I live. That's the New Year's Day holiday gazetted by the state government. :-) – RobG Apr 15 '19 at 07:55
  • Hey @RobG, why is moment.js consider a native js? – Shahar Shokrani Apr 15 '19 at 12:15
  • Any code written using pain ECMAScript is native javascript (well, native ECMAScript which is colloquially known as javascript). – RobG Apr 15 '19 at 12:48
  • I disagree, https://stackoverflow.com/questions/7022007/what-is-native-javascript – Shahar Shokrani Apr 15 '19 at 14:16
  • That link supports my comment, see the definition of a native object from EMCAScript 2011 (sometimes called ES5) which describes the *moment* object. Any code that uses plain ECMAScript without recourse to host objects is native ECMAScript. Moment.js **only** uses plain ECMAScript. If you disagree, please identify the host object dependencies. Of course the term "native object" hasn't been part of ECMA-262 for some time, but there is still [*NativeError*](http://ecma-international.org/ecma-262/9.0/#sec-native-error-types-used-in-this-standard). ;-) – RobG Apr 16 '19 at 10:35
  • I think that native JS is something you can write straight away inside the Chrome console for instance. Are moment object is available in Chrome console? – Shahar Shokrani Apr 16 '19 at 10:38
  • 1
    You can paste the moment.js source into the console and it runs. Any non–trivial code will have dependencies on other parts of the code, that doesn't make it non–native. Does use of modules make code non–native? Function declarations? Variable assignments? – RobG Apr 16 '19 at 10:55