简体   繁体   中英

Javascript algorithm that calculates week number in Fiscal Year

I have been looking for a Javascript algorithms that gives me the week number of a given Date object within a custom fiscal year. The fiscal year of my company starts on 1 September and ends on 31 August.

Say today happens to be September 1st and I pass in a newly instanced Date object to this function; I would expect it to return 1.

Hopefully someone will be able to help me with it.

Say you want the fiscal year for date X.

if X > September 1st
  week = week_nr(X) - week_nr(Sept 1st) + 1
else
  // We're in a new year
  week = week_nr(X) + week_nr(Sept 1st) - 1

That should do it - maybe there's a problem around new year, but easy enough to figure out I guess.

To get the week number within any year, there are plenty of solutions. Here's just one of them: http://syn.ac/tech/19/get-the-weeknumber-with-javascript/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM