1
|
|
|
// JavaScript Document |
2
|
|
View Code Duplication |
"use strict"; |
3
|
|
|
|
4
|
|
|
// https://stackoverflow.com/questions/3280323/get-week-of-the-month |
5
|
|
|
// return integer means the date in the which week of month |
6
|
|
|
// 6 means "Last", not "Sixth" |
7
|
|
|
// pass true to return the actual week of month |
8
|
|
|
Date.prototype.getWeekOfMonth = function(exact) { |
|
|
|
|
9
|
|
|
var month = this.getMonth() |
10
|
|
|
, year = this.getFullYear() |
11
|
|
|
, firstWeekday = new Date(year, month, 1).getDay() |
12
|
|
|
, lastDateOfMonth = new Date(year, month + 1, 0).getDate() |
13
|
|
|
, offsetDate = this.getDate() + firstWeekday - 1 |
14
|
|
|
, index = 0 // start index at 0 or 1, your choice |
15
|
|
|
, weeksInMonth = index + Math.ceil((lastDateOfMonth + firstWeekday - 7) / 7) |
16
|
|
|
, week = index + Math.floor(offsetDate / 7) |
17
|
|
|
; |
18
|
|
|
if (exact || week < 2 + index) return week; |
19
|
|
|
return week === weeksInMonth ? index + 5 : week; |
20
|
|
|
}; |
21
|
|
|
|
22
|
|
|
// https://stackoverflow.com/questions/7423801/given-a-weekday-and-the-day-of-the-month-it-occurs-can-i-get-its-ordinal-positi |
23
|
|
|
// https://stackoverflow.com/questions/28162140/how-to-find-ordinal-position-of-any-given-weekday-in-javascript/28163096#28163096 |
24
|
|
|
Date.prototype.nthofMonthStr = function(){ |
25
|
|
|
var today= this.getDate(),m=this.getMonth(), |
26
|
|
|
day= ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', |
27
|
|
|
'Friday', 'Saturday'][this.getDay()], |
28
|
|
|
month= ['January', 'February', 'March', 'April', 'May', 'June', |
29
|
|
|
'July', 'August', 'September', 'October', 'November', 'December'][m]; |
30
|
|
|
return [(m+1)+'-'+today,'the', (Math.ceil((today)/7)).nthStr(), day, 'of', month, 'in', this.getFullYear()].join(' '); |
31
|
|
|
} |
32
|
|
|
Date.prototype.nthofMonth = function(){ |
33
|
|
|
var today= this.getDate(),m=this.getMonth(), |
34
|
|
|
day= ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', |
|
|
|
|
35
|
|
|
'Friday', 'Saturday'][this.getDay()], |
36
|
|
|
month= ['January', 'February', 'March', 'April', 'May', 'June', |
|
|
|
|
37
|
|
|
'July', 'August', 'September', 'October', 'November', 'December'][m]; |
38
|
|
|
return (Math.ceil((today)/7)); |
39
|
|
|
} |
40
|
|
|
Number.prototype.nthStr = function(){ |
|
|
|
|
41
|
|
|
var n= Math.round(this), t= Math.abs(n%100), i= t%10; |
42
|
|
|
if(i<4 && (t<4 || t> 20)){ |
43
|
|
|
switch(i){ |
44
|
|
|
case 1:return n+'st'; |
45
|
|
|
case 2:return n+'nd'; |
46
|
|
|
case 3:return n+'rd'; |
47
|
|
|
} |
48
|
|
|
} |
49
|
|
|
return n+'th'; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
Object.defineProperty(Object.prototype, 'IsEmptyObject', { |
53
|
|
|
value : function() { |
54
|
|
|
var obj = this; |
55
|
|
|
for (var prop in obj) { |
56
|
|
|
if (obj.hasOwnProperty(prop)) |
57
|
|
|
return false; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
return true; |
61
|
|
|
}, |
62
|
|
|
enumerable : false |
63
|
|
|
}); |
64
|
|
|
String.prototype.IsNullOrEmpty = function(){ |
|
|
|
|
65
|
|
|
var curStr = this; |
66
|
|
|
var isNullOrEmpty = false; |
67
|
|
|
if(typeof(curStr) == "undefined") |
68
|
|
|
isNullOrEmpty = true; |
69
|
|
|
else if(curStr == null) |
70
|
|
|
isNullOrEmpty = true; |
71
|
|
|
else if(curStr == "") |
72
|
|
|
isNullOrEmpty = true; |
73
|
|
|
return isNullOrEmpty; |
74
|
|
|
} |
75
|
|
|
String.prototype.ReplaceAll = function(search, replacement) { |
76
|
|
|
var target = this; |
77
|
|
|
return target.replace(new RegExp(search, 'g'), replacement); |
78
|
|
|
}; |
79
|
|
|
|
80
|
|
|
function getFunctionName(fun) { |
81
|
|
|
var ret = fun.toString(); |
82
|
|
|
ret = ret.substr('function '.length); |
83
|
|
|
ret = ret.substr(0, ret.indexOf('(')); |
84
|
|
|
return ret; |
85
|
|
|
} |