jcal-zmanim 1.0.50 → 1.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (83) hide show
  1. package/LICENSE +674 -674
  2. package/{build → dist}/JCal/Dafyomi.d.ts +4 -3
  3. package/dist/JCal/Dafyomi.d.ts.map +1 -0
  4. package/{build → dist}/JCal/Dafyomi.js +9 -8
  5. package/dist/JCal/Dafyomi.js.map +1 -0
  6. package/{build → dist}/JCal/Location.d.ts +4 -4
  7. package/dist/JCal/Location.d.ts.map +1 -0
  8. package/{build → dist}/JCal/Location.js +10 -12
  9. package/dist/JCal/Location.js.map +1 -0
  10. package/{build → dist}/JCal/Molad.d.ts +1 -2
  11. package/dist/JCal/Molad.d.ts.map +1 -0
  12. package/{build → dist}/JCal/Molad.js +16 -19
  13. package/dist/JCal/Molad.js.map +1 -0
  14. package/{build → dist}/JCal/PirkeiAvos.d.ts +1 -0
  15. package/dist/JCal/PirkeiAvos.d.ts.map +1 -0
  16. package/{build → dist}/JCal/PirkeiAvos.js +4 -3
  17. package/dist/JCal/PirkeiAvos.js.map +1 -0
  18. package/{build → dist}/JCal/Sedra.d.ts +2 -1
  19. package/dist/JCal/Sedra.d.ts.map +1 -0
  20. package/{build → dist}/JCal/Sedra.js +2 -1
  21. package/dist/JCal/Sedra.js.map +1 -0
  22. package/{build → dist}/JCal/Zmanim.d.ts +1 -0
  23. package/dist/JCal/Zmanim.d.ts.map +1 -0
  24. package/{build → dist}/JCal/Zmanim.js +16 -16
  25. package/dist/JCal/Zmanim.js.map +1 -0
  26. package/{build/AppUtils.d.ts → dist/JCal/ZmanimUtils.d.ts} +10 -47
  27. package/dist/JCal/ZmanimUtils.d.ts.map +1 -0
  28. package/{build/AppUtils.js → dist/JCal/ZmanimUtils.js} +56 -122
  29. package/dist/JCal/ZmanimUtils.js.map +1 -0
  30. package/{build → dist}/JCal/jDate.d.ts +4 -3
  31. package/dist/JCal/jDate.d.ts.map +1 -0
  32. package/{build → dist}/JCal/jDate.js +24 -24
  33. package/dist/JCal/jDate.js.map +1 -0
  34. package/dist/Locations.d.ts +31 -0
  35. package/dist/Locations.d.ts.map +1 -0
  36. package/dist/Locations.js +119 -0
  37. package/dist/Locations.js.map +1 -0
  38. package/dist/Notifications.d.ts +18 -0
  39. package/dist/Notifications.d.ts.map +1 -0
  40. package/{build → dist}/Notifications.js +111 -130
  41. package/dist/Notifications.js.map +1 -0
  42. package/dist/README.md +521 -0
  43. package/{build/JCal → dist}/Utils.d.ts +72 -10
  44. package/dist/Utils.d.ts.map +1 -0
  45. package/{build/JCal → dist}/Utils.js +176 -70
  46. package/dist/Utils.js.map +1 -0
  47. package/{build → dist}/ZmanTypes.d.ts +11 -4
  48. package/dist/ZmanTypes.d.ts.map +1 -0
  49. package/{build → dist}/ZmanTypes.js +12 -5
  50. package/dist/ZmanTypes.js.map +1 -0
  51. package/dist/index.d.ts +2 -0
  52. package/dist/index.d.ts.map +1 -0
  53. package/dist/index.js +43 -0
  54. package/dist/index.js.map +1 -0
  55. package/dist/locations.json +1 -0
  56. package/package.json +43 -33
  57. package/README.md +0 -2
  58. package/build/GeneralUtils.d.ts +0 -43
  59. package/build/GeneralUtils.js +0 -93
  60. package/build/Locations.d.ts +0 -7
  61. package/build/Locations.js +0 -1308
  62. package/build/Notifications.d.ts +0 -14
  63. package/build/Settings.d.ts +0 -25
  64. package/build/Settings.js +0 -75
  65. package/build/index.d.ts +0 -1
  66. package/build/index.js +0 -59
  67. package/src/AppUtils.ts +0 -500
  68. package/src/GeneralUtils.ts +0 -84
  69. package/src/JCal/Dafyomi.ts +0 -139
  70. package/src/JCal/Location.ts +0 -100
  71. package/src/JCal/Molad.ts +0 -105
  72. package/src/JCal/PirkeiAvos.ts +0 -180
  73. package/src/JCal/Sedra.ts +0 -215
  74. package/src/JCal/Utils.ts +0 -732
  75. package/src/JCal/Zmanim.ts +0 -270
  76. package/src/JCal/jDate.ts +0 -714
  77. package/src/Locations.ts +0 -1303
  78. package/src/Notifications.ts +0 -1243
  79. package/src/Settings.ts +0 -103
  80. package/src/ZmanTypes.ts +0 -184
  81. package/src/index.ts +0 -31
  82. package/src/jcal-zmanim.d.ts +0 -4
  83. package/tsconfig.json +0 -109
package/src/JCal/jDate.ts DELETED
@@ -1,714 +0,0 @@
1
- import { isString, isNumber, has, isValidDate } from '../GeneralUtils';
2
- import Utils from './Utils.js';
3
- import Sedra from './Sedra.js';
4
- import PirkeiAvos from './PirkeiAvos.js';
5
- import Zmanim from './Zmanim.js';
6
- import DafYomi from './Dafyomi';
7
- import Location from './Location';
8
-
9
- /** Keeps a "memcache" of years that have had their elapsed days previously calculated. Format: { year:5776, elapsed:2109283 } */
10
- const _yearCache: [{ year: number, elapsed: number }?] = [],
11
- //The absolute date for the zero hour of all javascript date objects - 1/1/1970 0:00:00 UTC
12
- JS_START_DATE_ABS = 719163,
13
- //The number of milliseconds in everyday
14
- MS_PER_DAY = 8.64e7,
15
- //The time zone offset (in minutes) for 1/1/1970 0:00:00 UTC at the current users time zone
16
- JS_START_OFFSET = new Date(0).getTimezoneOffset();
17
- /* ****************************************************************************************************************
18
- * Many of the date conversion algorithms in the jDate class are based on the C code which was translated from Lisp
19
- * in "Calendrical Calculations" by Nachum Dershowitz and Edward M. Reingold
20
- * in Software---Practice & Experience, vol. 20, no. 9 (September, 1990), pp. 899--928.
21
- * ****************************************************************************************************************/
22
-
23
- /** Represents a single day in the Jewish Calendar. */
24
- export default class jDate {
25
- public Day: number;
26
- public Month: number;
27
- public Year: number;
28
- public Abs: number;
29
-
30
- /**
31
- * Create a new Jdate.
32
- * new jDate() - Sets the Jewish Date for the current system date
33
- * new jDate(javascriptDateObject) - Sets to the Jewish date on the given Gregorian date
34
- * new jDate("January 1 2045") - Accepts any valid javascript Date string (uses javascripts new Date(string))
35
- * new jDate(jewishYear, jewishMonth, jewishDay) - Months start at 1. Nissan is month 1 Adar Sheini is 13.
36
- * new jDate(jewishYear, jewishMonth) - Same as above, with Day defaulting to 1
37
- * new jDate( { year: 5776, month: 4, day: 5 } ) - same as new jDate(jewishYear, jewishMonth, jewishDay)
38
- * new jDate( { year: 5776, month: 4 } ) - same as new jDate(jewishYear, jewishMonth)
39
- * new jDate( { year: 5776 } ) - sets to the first day of Rosh Hashana on the given year
40
- * new jDate(absoluteDate) - The number of days elapsed since the theoretical date Sunday, December 31, 0001 BCE
41
- * new jDate(jewishYear, jewishMonth, jewishDay, absoluteDate) - Most efficient constructor. Needs no calculations at all.
42
- * new jDate( { year: 5776, month: 4, day: 5, abs: 122548708 } ) - same as new jDate(jewishYear, jewishMonth, jewishDay, absoluteDate)
43
- * @param {number | Date | string | {year:number,month:number,day:number} | [number, number, number, number]} [arg] The full Jewish year number OR Javascript Date object or string OR object or array of year, month, day
44
- * @param {number} [month] The month of the Jewish date. Nissan is 1.
45
- * @param {number} [day] The day of the month
46
- * @param {number} [abs] The number of days that have passed since 12/31/0001
47
- */
48
- constructor(arg?: number | Date | string | { year: number, month: number, day: number } | [number, number, number, number], month?: number, day?: number, abs?: number) {
49
- //The day of the Jewish Month
50
- this.Day = NaN;
51
- //The Jewish Month. As in the Torah, Nissan is 1 and Adara Sheini is 13
52
- this.Month = NaN;
53
- //The number of years since the creation of the world
54
- this.Year = NaN;
55
- //The number of days since the theoretical date: Dec. 31, 0001 BCE
56
- this.Abs = NaN;
57
-
58
- if (arguments.length === 0) {
59
- this.fromAbs(jDate.absSd(new Date()));
60
- } else if (arg instanceof Date) {
61
- if (isValidDate(arg)) {
62
- this.fromAbs(jDate.absSd(arg));
63
- } else {
64
- throw 'jDate constructor: The given Date is not a valid javascript Date';
65
- }
66
- } else if (Array.isArray(arg) && arg.length >= 3) {
67
- this.Day = arg[0];
68
- this.Month = arg[1];
69
- this.Year = arg[2];
70
- this.Abs =
71
- (arg.length > 3 && arg[3]) ||
72
- jDate.absJd(this.Year, this.Month, this.Day);
73
- } else if (arg && isString(arg)) {
74
- const d = new Date(arg as string);
75
- if (isValidDate(d)) {
76
- this.fromAbs(jDate.absSd(d));
77
- } else {
78
- throw (
79
- 'jDate constructor: The given string "' +
80
- arg +
81
- '" cannot be parsed into a Date'
82
- );
83
- }
84
- } else if (isNumber(arg)) {
85
- //if no other arguments were supplied, we assume that the supplied number is an absolute date
86
- if (arguments.length === 1) {
87
- this.fromAbs(arg as number);
88
- }
89
- //If the year and any other number is supplied, we set the year and create the date using either the supplied values or the defaults
90
- else {
91
- this.Year = arg as number;
92
- this.Month = month || 7; //If no month was supplied, we take Tishrei
93
- this.Day = day || 1; //If no day was supplied, we take the first day of the month
94
- //If the absolute date was also supplied (very efficient), we use the supplied value, otherwise we calculate it.
95
- this.Abs = abs || jDate.absJd(this.Year, this.Month, this.Day);
96
- }
97
- }
98
- //If arg is an object that has a "year" property that contains a valid value...
99
- else if (typeof arg === 'object') {
100
- const argObj = arg as { year: number, month: number, day: number, abs: number };
101
- if (isNumber(argObj.year)) {
102
- this.Day = argObj.day || 1;
103
- this.Month = argObj.month || 7;
104
- this.Year = argObj.year;
105
- this.Abs = argObj.abs || jDate.absJd(this.Year, this.Month, this.Day);
106
- }
107
- }
108
- }
109
-
110
- /**Sets the current Jewish date from the given absolute date*/
111
- fromAbs(absolute: number) {
112
- const ymd = jDate.fromAbs(absolute);
113
- this.Year = ymd.year;
114
- this.Month = ymd.month;
115
- this.Day = ymd.day;
116
- this.Abs = absolute;
117
- }
118
-
119
- /**Returns a valid javascript Date object that represents the Gregorian date
120
- that starts at midnight of the current Jewish date.*/
121
- getDate(): Date {
122
- return jDate.sdFromAbs(this.Abs);
123
- }
124
- /**
125
- * @returns {number} The day of the week for the current Jewish date. Sunday is 0 and Shabbos is 6.
126
- */
127
- getDayOfWeek(): number {
128
- return Math.abs(this.Abs % 7);
129
- }
130
- /**
131
- * @returns {number} The day of the week for the current Jewish date. Sunday is 0 and Shabbos is 6
132
- */
133
- get DayOfWeek(): number {
134
- return this.getDayOfWeek();
135
- }
136
- /**Returns a new Jewish date represented by adding the given number of days to the current Jewish date.*/
137
- addDays(days: number): jDate {
138
- return new jDate(this.Abs + days);
139
- }
140
- /**
141
- * Returns a new Jewish date represented by adding the given number of
142
- * Jewish Months to the current Jewish date.
143
- * If the current Day is 30 and the new month only has 29 days,
144
- * the 29th day of the month is returned.
145
- * @param {number} months
146
- */
147
- addMonths(months: number): jDate {
148
- let year = this.Year,
149
- month = this.Month,
150
- day = this.Day,
151
- miy = jDate.monthsJYear(year);
152
-
153
- for (let i = 0; i < Math.abs(months); i++) {
154
- if (months > 0) {
155
- month += 1;
156
- if (month > miy) {
157
- month = 1;
158
- }
159
- if (month === 7) {
160
- year += 1;
161
- miy = jDate.monthsJYear(year);
162
- }
163
- } else if (months < 0) {
164
- month -= 1;
165
- if (month === 0) {
166
- month = miy;
167
- }
168
- if (month === 6) {
169
- year -= 1;
170
- miy = jDate.monthsJYear(year);
171
- }
172
- }
173
- }
174
- if (day === 30 && jDate.daysJMonth(year, month) === 29) {
175
- day = 29;
176
- }
177
-
178
- return new jDate(year, month, day);
179
- }
180
- /**
181
- * Returns a new Jewish date represented by adding the
182
- * given number of Jewish Years to the current Jewish date.
183
- * If the current Day is 30 and the new dates month only has 29 days,
184
- * the 29th day of the month is returned.
185
- * @param {number} years
186
- */
187
- addYears(years: number): jDate {
188
- let year = this.Year + years,
189
- month = this.Month,
190
- day = this.Day;
191
-
192
- if (month === 13 && !jDate.isJdLeapY(year)) {
193
- month = 12;
194
- } else if (month === 8 && day === 30 && !jDate.isLongCheshvan(year)) {
195
- month = 9;
196
- day = 1;
197
- } else if (month === 9 && day === 30 && jDate.isShortKislev(year)) {
198
- month = 10;
199
- day = 1;
200
- }
201
-
202
- if (day === 30 && jDate.daysJMonth(year, month) === 29) {
203
- day = 29;
204
- }
205
-
206
- return new jDate(year, month, day);
207
- }
208
- addSecularMonths(months: number): jDate {
209
- const secDate = new Date(this.getDate().valueOf());
210
- secDate.setMonth(secDate.getMonth() + months);
211
- return new jDate(secDate);
212
- }
213
- addSecularYears(years: number): jDate {
214
- const secDate = new Date(this.getDate().valueOf());
215
- secDate.setFullYear(secDate.getFullYear() + years);
216
- return new jDate(secDate);
217
- }
218
- /**Gets the number of days separating this Jewish Date and the given one.
219
- *
220
- * If the given date is before this one, the number will be negative.
221
- * @param {jDate} jd
222
- * */
223
- diffDays(jd: jDate): number {
224
- return jd.Abs - this.Abs;
225
- }
226
-
227
- /**Gets the number of months separating this Jewish Date and the given one.
228
- *
229
- * Ignores the Day property:
230
- *
231
- * jDate.toJDate(5777, 6, 29).diffMonths(jDate.toJDate(5778, 7, 1)) will return 1 even though they are a day apart.
232
- *
233
- * If the given date is before this one, the number will be negative.
234
- * @param {jDate} jd
235
- * */
236
- diffMonths(jd: jDate): number {
237
- let month = jd.Month,
238
- year = jd.Year,
239
- months = 0;
240
-
241
- while (!(year === this.Year && month === this.Month)) {
242
- if (this.Abs > jd.Abs) {
243
- months--;
244
- month++;
245
- if (month > jDate.monthsJYear(year)) {
246
- month = 1;
247
- } else if (month === 7) {
248
- year++;
249
- }
250
- } else {
251
- months++;
252
- month--;
253
- if (month < 1) {
254
- month = jDate.monthsJYear(year);
255
- } else if (month === 6) {
256
- year--;
257
- }
258
- }
259
- }
260
-
261
- return months;
262
- }
263
-
264
- /**Gets the number of years separating this Jewish Date and the given one.
265
- *
266
- * Ignores the Day and Month properties:
267
- *
268
- * jDate.toJDate(5777, 6, 29).diffYears(jDate.toJDate(5778, 7, 1)) will return 1 even though they are a day apart.
269
- *
270
- * If the given date is before this one, the number will be negative.
271
- * @param {jDate} jd*/
272
- diffYears(jd: jDate): number {
273
- return jd.Year - this.Year;
274
- }
275
-
276
- /**
277
- * Returns the current Jewish date in the format: Thursday, the 3rd of Kislev 5776.
278
- * @param {boolean} hideDayOfWeek
279
- * @param {boolean} dontCapitalize
280
- */
281
- toString(hideDayOfWeek?: boolean, dontCapitalize?: boolean): string {
282
- return (
283
- (hideDayOfWeek
284
- ? dontCapitalize
285
- ? 't'
286
- : 'T'
287
- : Utils.dowEng[this.getDayOfWeek()] + ', t') +
288
- 'he ' +
289
- Utils.toSuffixed(this.Day) +
290
- ' of ' +
291
- this.monthName()
292
- );
293
- }
294
-
295
- /**
296
- * Returns the current Jewish date in the format "[Tuesday] Nissan 3, 5778"
297
- * @param {boolean} showDow - show day of week?
298
- */
299
- toShortstring(showDow: boolean): string {
300
- return (
301
- (showDow ? Utils.dowEng[this.getDayOfWeek()] + ' ' : '') +
302
- Utils.jMonthsEng[this.Month] +
303
- ' ' +
304
- this.Day.toString() +
305
- ', ' +
306
- this.Year.toString()
307
- );
308
- }
309
-
310
- /**
311
- * Returns the current Jewish date in the format "Nissan 5778"
312
- * @param {boolean} showYear - show the year number?
313
- */
314
- monthName(showYear = true): string {
315
- return (
316
- Utils.jMonthsEng[this.Month] +
317
- (showYear ? ' ' + this.Year.toString() : '')
318
- );
319
- }
320
-
321
- /**Returns the current Jewish date in the format: יום חמישי כ"א כסלו תשע"ו.*/
322
- toStringHeb(): string {
323
- return (
324
- Utils.dowHeb[this.getDayOfWeek()] +
325
- ' ' +
326
- Utils.toJNum(this.Day) +
327
- ' ' +
328
- Utils.jMonthsHeb[this.Month] +
329
- ' ' +
330
- Utils.toJNum(this.Year % 1000)
331
- );
332
- }
333
-
334
- /**Gets the day of the omer for the current Jewish date. If the date is not during sefira, 0 is returned.*/
335
- getDayOfOmer(): number {
336
- let dayOfOmer = 0;
337
- if (
338
- (this.Month === 1 && this.Day > 15) ||
339
- this.Month === 2 ||
340
- (this.Month === 3 && this.Day < 6)
341
- ) {
342
- const first = new jDate(this.Year, 1, 15);
343
- dayOfOmer = first.diffDays(this);
344
- }
345
- return dayOfOmer;
346
- }
347
-
348
- /**
349
- * Returns true if this day is yomtov or chol hamoed
350
- * @param {boolean} israel
351
- */
352
- isYomTovOrCholHamoed(israel: boolean): boolean {
353
- return (
354
- this.isYomTov(israel) ||
355
- (this.Month === 1 && [16, 17, 18, 19, 20].includes(this.Day)) ||
356
- (this.Month === 7 && [16, 17, 18, 19, 20, 21].includes(this.Day))
357
- );
358
- }
359
-
360
- /**
361
- * Returns true if this day is yomtov
362
- * @param {boolean} israel
363
- */
364
- isYomTov(israel: boolean): boolean {
365
- const day = this.Day;
366
- switch (this.Month) {
367
- case 1:
368
- if (day === 15 || day === 21) return true;
369
- if (!israel && (day === 16 || day === 22)) return true;
370
- break;
371
- case 3:
372
- if (day === 6 || (!israel && day === 7)) return true;
373
- break;
374
- case 7:
375
- if ([1, 2, 10, 15, 22].includes(day)) {
376
- return true;
377
- }
378
- if (!israel && (day === 16 || day === 23)) return true;
379
- break;
380
- }
381
- return false;
382
- }
383
-
384
- /**Is today Erev Yom Tov? (includes Erev second days of Sukkos and Pesach) */
385
- isErevYomTov(): boolean {
386
- return (
387
- (this.Month === 1 && has(this.Day, 14, 20)) ||
388
- (this.Month === 3 && this.Day === 5) ||
389
- (this.Month === 6 && this.Day === 29) ||
390
- (this.Month === 7 && has(this.Day, 9, 14, 21))
391
- );
392
- }
393
-
394
- /**Does the current Jewish date have candle lighting before sunset?*/
395
- hasCandleLighting(): boolean {
396
- const dow = this.getDayOfWeek();
397
-
398
- if (dow === 5) {
399
- return true;
400
- } else if (dow === 6) {
401
- //there is no "candle lighting time" - even if yom tov is on Motzai Shabbos
402
- return false;
403
- }
404
-
405
- return this.isErevYomTov();
406
- }
407
-
408
- /**Is the current Jewish Date the day before a yomtov that contains a Friday?*/
409
- hasEiruvTavshilin(israel: boolean): boolean {
410
- let dow = this.getDayOfWeek();
411
- return (
412
- //Eiruv Tavshilin is only on Wednesday or Thursday
413
- [3, 4].includes(dow) &&
414
- //today is Erev Yomtov
415
- this.isErevYomTov() &&
416
- //Thursday OR Wednesday when in Chu"l or Erev Rosh Hashana anywhere
417
- (dow === 4 || (dow === 3 && (!israel || this.Month === 6))) &&
418
- //No Eiruv Tavshilin on Erev yom kippur
419
- this.Day !== 9
420
- );
421
- }
422
-
423
- /**Gets the candle lighting time for the current Jewish date for the given Location object.*/
424
- getCandleLighting(location: Location, nullIfNoCandles: boolean) {
425
- if (!location) {
426
- throw 'To get sunrise and sunset, the location needs to be supplied';
427
- }
428
- if (this.hasCandleLighting()) {
429
- return Zmanim.getCandleLighting(this, location);
430
- } else if (nullIfNoCandles) {
431
- return null;
432
- } else {
433
- throw 'No candle lighting on ' + this.toString();
434
- }
435
- }
436
-
437
- /**Get the sedra of the week for the current Jewish date.*/
438
- getSedra(israel: boolean) {
439
- return new Sedra(this, israel);
440
- }
441
-
442
- /**Get the prakim of Pirkei Avos for the current Jewish date.*/
443
- getPirkeiAvos(israel: boolean) {
444
- return PirkeiAvos.getPrakim(this, israel);
445
- }
446
-
447
- /**Gets sunrise and sunset time for the current Jewish date at the given Location.
448
- *
449
- * Return format: {sunrise: {hour: 6, minute: 18}, sunset: {hour: 19, minute: 41}}*/
450
- getSunriseSunset(location:Location, ignoreElevation?: boolean) {
451
- if (!location) {
452
- throw 'To get sunrise and sunset, the location needs to be supplied';
453
- }
454
- return Zmanim.getSunTimes(this, location, !ignoreElevation);
455
- }
456
-
457
- /**Gets Chatzos for both the day and the night for the current Jewish date at the given Location.
458
- *
459
- *Return format: {hour: 11, minute: 48}*/
460
- getChatzos(location:Location) {
461
- if (!location) {
462
- throw 'To get Chatzos, the location needs to be supplied';
463
- }
464
- return Zmanim.getChatzos(this, location);
465
- }
466
-
467
- /**Gets the length of a single Sha'a Zmanis in minutes for the current Jewish date at the given Location.*/
468
- getShaaZmanis(location:Location, offset: number) {
469
- if (!location) {
470
- throw 'To get the Shaa Zmanis, the location needs to be supplied';
471
- }
472
- return Zmanim.getShaaZmanis(this, location, offset);
473
- }
474
-
475
- /**Returns the daily daf in English. For example: Sukkah, Daf 3.*/
476
- getDafYomi() {
477
- return DafYomi.toString(this)||'';
478
- }
479
-
480
- /**Gets the daily daf in Hebrew. For example: 'סוכה דף כ.*/
481
- getDafyomiHeb() {
482
- return DafYomi.toStringHeb(this)||'';
483
- }
484
-
485
- /**
486
- * Converts its argument/s to a Jewish Date.
487
- * Samples of use:
488
- * To get the current Jewish Date: jDate.toJDate(new Date()).
489
- * To print out the current date in English: jDate.toJDate(new Date()).toString()
490
- * To print out the current date in Hebrew: jDate.toJDate(new Date()).toStringHeb()
491
- *
492
- * Arguments to the jDate.toJDate function can be one of the following:
493
- * jDate.toJDate() - Sets the Jewish Date for the current system date
494
- * jDate.toJDate(Date) - Sets to the Jewish date on the given Javascript Date object
495
- * jDate.toJDate("January 1 2045") - Accepts any valid Javascript Date string (uses string constructor of Date object)
496
- * jDate.toJDate(jewishYear, jewishMonth, jewishDay) - Months start at 1. Nissan is month 1 Adara Sheini is 13.
497
- * jDate.toJDate(jewishYear, jewishMonth) - Same as above, with Day defaulting to 1
498
- * jDate.toJDate(jewishYear) - sets to the first day of Rosh Hashana on the given year
499
- * jDate.toJDate( { year: 5776, month: 4, day: 5 } ) - Months start at 1. Nissan is month 1 Adara Sheini is 13.
500
- * jDate.toJDate( { year: 5776, month: 4 } ) - Same as above, with Day defaulting to 1
501
- * jDate.toJDate( { year: 5776 } ) - sets to the first day of Rosh Hashana on the given year
502
- * jDate.toJDate(jewishYear, jewishMonth, jewishDay, absoluteDate) - Most efficient. Needs no calculations at all. The absoluteDate is the number of days elapsed since the theoretical date Sunday, December 31, 0001 BCE.
503
- * jDate.toJDate( { year: 5776, month: 4, day: 5, abs: 122548708 } ) - same as jDate.toJDate(jewishYear, jewishMonth, jewishDay, absoluteDate)
504
- ****************************************************************************************************************/
505
- static toJDate(arg?: number | Date | string | { year: number, month: number, day: number } | [number, number, number, number], month?: number, day?: number, abs?: number): jDate {
506
- if (arguments.length === 0) {
507
- return new jDate();
508
- }
509
- // If just the year is set, then the date is set to Rosh Hashana of that year.
510
- // In the above scenario, we can't just pass the args along, as the constructor will treat it as an absolute date.
511
- //...and that folks, is actually the whole point of this function...
512
- else if (isNumber(arg) && arguments.length === 1) {
513
- return new jDate(arg, 7, 1);
514
- } else {
515
- return new jDate(arg, month, day, abs);
516
- }
517
- }
518
-
519
- static now(): jDate {
520
- return new jDate();
521
- }
522
-
523
- /**Calculate the Jewish year, month and day for the given absolute date.*/
524
- static fromAbs(absDay: number): { year: number, month: number, day: number } {
525
- //To save on calculations, start with a few years before date
526
- let year = 3761 + Utils.toInt(absDay / (absDay > 0 ? 366 : 300)),
527
- month: number,
528
- day: number;
529
-
530
- // Search forward for year from the approximation year.
531
- while (absDay >= jDate.absJd(year + 1, 7, 1)) {
532
- year++;
533
- }
534
- // Search forward for month from either Tishrei or Nissan.
535
- month = absDay < jDate.absJd(year, 1, 1) ? 7 : 1;
536
- while (
537
- absDay > jDate.absJd(year, month, jDate.daysJMonth(year, month))
538
- ) {
539
- month++;
540
- }
541
- // Calculate the day by subtraction.
542
- day = absDay - jDate.absJd(year, month, 1) + 1;
543
-
544
- return { year, month, day };
545
- }
546
- /**
547
- * Gets the absolute date of the given javascript Date object.
548
- * @param {Date} date
549
- */
550
- static absSd(date: Date): number {
551
- //Get the correct number of milliseconds since 1/1/1970 00:00:00 UTC until current system time
552
- const ms = date.valueOf() - date.getTimezoneOffset() * 60000,
553
- //The number of full days since 1/1/1970.
554
- numFullDays = Math.floor(ms / MS_PER_DAY);
555
- //Add that to the number of days from 1/1/0001 until 1/1/1970 00:00:00 UTC
556
- return JS_START_DATE_ABS + numFullDays;
557
- }
558
-
559
- /**Calculate the absolute date for the given Jewish Date.*/
560
- static absJd(year: number, month: number, day: number): number {
561
- //The number of total days.
562
- let dayInYear = day; // day is the number of days so far this month.
563
- if (month < 7) {
564
- // Before Tishrei, so add days in prior months this year before and after Nissan.
565
- let m = 7;
566
- while (m <= jDate.monthsJYear(year)) {
567
- dayInYear += jDate.daysJMonth(year, m);
568
- m++;
569
- }
570
- m = 1;
571
- while (m < month) {
572
- dayInYear += jDate.daysJMonth(year, m);
573
- m++;
574
- }
575
- } else {
576
- // Add days in prior months this year
577
- let m = 7;
578
- while (m < month) {
579
- dayInYear += jDate.daysJMonth(year, m);
580
- m++;
581
- }
582
- }
583
- // Days elapsed before absolute date 1. - Days in prior years.
584
- return dayInYear + (jDate.tDays(year) + -1373429);
585
- }
586
-
587
- /**
588
- * Gets a javascript date from an absolute date
589
- */
590
- static sdFromAbs(abs: number): Date {
591
- //The "zero hour" for Javascript is 1/1/1970 0:00:00 UTC.
592
- //If the time zone offset was more than 0, the current time zone was earlier than UTC at the time.
593
- //As the "zero hour" is at midnight, so if the current time was earlier than that, than it was during the previous date.
594
- //So we will need to add another day to get the correct date.
595
- const offset = JS_START_OFFSET > 0 ? 1 : 0,
596
- //Get the number of days from the "zero hour" until the given date.
597
- //This is done by taking the given absolute date and removing the
598
- //number of days from absolute date 0 until the js "zero hour" - keeping into
599
- //account the previously calculated possible day offset.
600
- daysSinceStart = abs - JS_START_DATE_ABS + offset;
601
- //Create a javascript date from the number of milliseconds since the "zero hour"
602
- return new Date(daysSinceStart * MS_PER_DAY);
603
- }
604
-
605
- /**number of days in the given Jewish Month. Nissan is 1 and Adar Sheini is 13.*/
606
- static daysJMonth(year: number, month: number): number {
607
- switch (month) {
608
- //Nissan, Sivan, Av, Tishrei and Shvat always have 30 days
609
- case 1:
610
- case 3:
611
- case 5:
612
- case 7:
613
- case 11:
614
- return 30;
615
- //Iyyar, Tammuz, Ellul, Teves and Adar Sheini always have 29 days.
616
- case 2:
617
- case 4:
618
- case 6:
619
- case 10:
620
- case 13:
621
- return 29;
622
- //Cheshvan sometimes has 29 days and sometimes 30 days.
623
- case 8:
624
- return jDate.isLongCheshvan(year) ? 30 : 29;
625
- //Kislev sometimes has 29 days and sometimes 30 days.
626
- case 9:
627
- return jDate.isShortKislev(year) ? 29 : 30;
628
- //Adar has 29 days unless it is Adar Rishon.
629
- case 12:
630
- return jDate.isJdLeapY(year) ? 30 : 29;
631
- }
632
- return 0;
633
- }
634
-
635
- /**Elapsed days since creation of the world until Rosh Hashana of the given year*/
636
- static tDays(year: number): number {
637
- /*As this function is called many times, often on the same year for all types of calculations,
638
- we save a list of years with their elapsed values.*/
639
- const cached = _yearCache.find(y => !!y && y.year === year);
640
- //If this year was already calculated and cached, then we return the cached value.
641
- if (cached) {
642
- return cached.elapsed;
643
- }
644
-
645
- const months = Utils.toInt(
646
- 235 * Utils.toInt((year - 1) / 19) + // Leap months this cycle
647
- 12 * ((year - 1) % 19) + // Regular months in this cycle.
648
- (7 * ((year - 1) % 19) + 1) / 19,
649
- ), // Months in complete cycles so far.
650
- parts = 204 + 793 * (months % 1080),
651
- hours =
652
- 5 +
653
- 12 * months +
654
- 793 * Utils.toInt(months / 1080) +
655
- Utils.toInt(parts / 1080),
656
- conjDay = Utils.toInt(1 + 29 * months + hours / 24),
657
- conjParts = 1080 * (hours % 24) + (parts % 1080);
658
-
659
- let altDay;
660
- /* at the end of a leap year - 15 hours, 589 parts or later... -
661
- ... or is on a Monday at... - ...of a common year, -
662
- at 9 hours, 204 parts or later... - ...or is on a Tuesday... -
663
- If new moon is at or after midday,*/
664
- if (
665
- conjParts >= 19440 ||
666
- (conjDay % 7 === 2 &&
667
- conjParts >= 9924 &&
668
- !jDate.isJdLeapY(year)) ||
669
- (conjDay % 7 === 1 &&
670
- conjParts >= 16789 &&
671
- jDate.isJdLeapY(year - 1))
672
- ) {
673
- // Then postpone Rosh HaShanah one day
674
- altDay = conjDay + 1;
675
- } else {
676
- altDay = conjDay;
677
- }
678
-
679
- // A day is added if Rosh HaShanah would occur on Sunday, Friday or Wednesday,
680
- if (has(altDay % 7, 0, 3, 5)) {
681
- altDay += 1;
682
- }
683
-
684
- //Add this year to the cache to save on calculations later on
685
- _yearCache.push({ year: year, elapsed: altDay });
686
-
687
- return altDay;
688
- }
689
-
690
- /**number of days in the given Jewish Year.*/
691
- static daysJYear(year: number): number {
692
- return jDate.tDays(year + 1) - jDate.tDays(year);
693
- }
694
-
695
- /**Does Cheshvan for the given Jewish Year have 30 days?*/
696
- static isLongCheshvan(year: number): boolean {
697
- return jDate.daysJYear(year) % 10 === 5;
698
- }
699
-
700
- /**Does Kislev for the given Jewish Year have 29 days?*/
701
- static isShortKislev(year: number): boolean {
702
- return jDate.daysJYear(year) % 10 === 3;
703
- }
704
-
705
- /**Does the given Jewish Year have 13 months?*/
706
- static isJdLeapY(year: number): boolean {
707
- return (7 * year + 1) % 19 < 7;
708
- }
709
-
710
- /**number of months in Jewish Year.*/
711
- static monthsJYear(year: number): number {
712
- return jDate.isJdLeapY(year) ? 13 : 12;
713
- }
714
- }