PageSourceSearch

https://www.igoshogi.net/share-v2/js/date-format.js

js igoshogi.net collected 2026-10-02 14:59:48 UTC 4,763 bytes, 191 lines download raw bytes

1/** 日付オブジェクトを、 yyyy.mm.dd 形式の文字列にする
2 * @param	Date	date
3 * @return	string			'YYYY.mm.dd'
4 */
5const dateFormat = (date) => { // eslint-disable-line no-unused-vars
6	if( ! date ) {
7		return "";
8  }
9	const y = date.getFullYear();
10	const m = ("0" + (date.getMonth() + 1).toString()).slice(-2);
11	const d = ("0" + (date.getDate()).toString()).slice(-2);
12	return y+"."+m+"."+d;
13};
14
15class DateHelper // eslint-disable-line no-unused-vars
16{
17	static getDateObj(dateObj) {
18		// 日付を文字列で指定された場合
19		if( typeof dateObj === 'string' ) {
20			dateObj = dateObj.replace(/([\d]+-[\d]+-[\d]+) ([\d]+:[\d]+:[\d]+).*/, '$1T$2+0900');
21			dateObj = new Date( dateObj );
22		}
23		return dateObj;
24	}
25
26	static format( dateObj, formatStr = 'yyyy-mm-dd hh:ii:ss' )
27  {
28    const dObj = DateHelper.getDateObj( dateObj );
29
30    const d = DateHelper.getParams( (dObj) ? dObj: new Date() );
31
32		const l2 = (s) => (' 00'+s).slice(-2);
33
34    return formatStr.replace('yyyy', d.y)
35              .replace('yy', (""+d.y).slice(-2))
36              .replace('mm', l2(d.m))
37              .replace('m' , d.m)
38              .replace('dd', l2(d.d))
39              .replace('d' , d.d)
40              .replace('hh', l2(d.h))
41              .replace('h' , d.h)
42              .replace('ii', l2(d.i))
43              .replace('i' , d.i)
44              .replace('ss', l2(d.s))
45              .replace('s' , d.s);
46	}
47
48	static getParams( dateObj )
49  {
50		return {
51			'y': dateObj.getFullYear(),
52			'm': (dateObj.getMonth() + 1),
53			'd': dateObj.getDate(),
54			'h': dateObj.getHours(),
55			'i': dateObj.getMinutes(),
56			's': dateObj.getSeconds()
57		};
58	}
59
60  static firstDayOfMonth( dateObj )
61  {
62    return new Date( dateObj.getFullYear(), dateObj.getMonth(), 1 );
63  }
64
65  static lastDayOfMonth( dateObj )
66  {
67    return new Date( dateObj.getFullYear(), dateObj.getMonth() + 1, 0 );
68  }
69
70  static daysAfter( dateObj, days )
71  {
72    return new Date( dateObj.getTime() + days * 24 * 60 * 60 * 1000 );
73  }
74
75	static getWeekJ( dateObj )
76  {
77    const dayIndex = dateObj.getDay();
78    return ["日","月","火","水","木","金","土"][dayIndex];
79	}
80
81	static getWeekE( dateObj )
82  {
83    const weekIndex = dateObj.getDay();
84    return [
85      "Sunday",
86      "Monday",
87      "Tuesday",
88      "Wednesday",
89      "Thursday",
90      "Friday",
91      "Saturday"
92    ][weekIndex];
93	}
94
95	static getMonthE( numMonth )
96  {
97		return [
98      'Jan',
99      'Feb',
100      'Mar',
101      'Apr',
102      'May',
103      'Jun',
104      'Jul',
105      'Aug',
106      'Sep',
107      'Oct',
108      'Nov',
109      'Dec'
110    ][ parseInt( numMonth ) - 1 ];
111	}
112
113	static getMonthFullE( numMonth )
114  {
115		return [
116      'January',
117      'February',
118      'March',
119      'April',
120      'May',
121      'June',
122      'July',
123      'August',
124      'September',
125      'October',
126      'November',
127      'December'
128    ][ parseInt( numMonth ) - 1 ];
129	}
130
131	static getYearAndMonthE( dateObj )
132  {
133		return DateHelper.getMonthFullE( dateObj.getMonth() + 1 ) + ", " + dateObj.getFullYear();
134	}
135
136	static getFullDateStrE( dateObj )
137  {
138		return DateHelper.getWeekE( dateObj )
139            + ", "
140            + DateHelper.getMonthFullE( dateObj.getMonth() + 1 )
141            + " "
142            + dateObj.getDate()
143            + ", "
144            + dateObj.getFullYear();
145	}
146
147	/** yyyymmdd to yyyy.mm.dd */
148	static getColumnDate(d)
149  {
150		return d.slice(0,4) + '.' + d.slice(4,2) + '.' + d.slice(-2);
151	}
152
153	/** Date to yyyymmdd */
154	static getSerialDate(dateObj)
155  {
156    const l2 = (s) => ('00'+s.toString()).slice(-2);
157
158    const d = DateHelper.getParams( dateObj );
159		return d.y + l2(d.m) + l2(d.d);
160	}
161
162	static getSerialDateTime(dateObj)
163  {
164    const l2 = (s) => ('00'+s.toString()).slice(-2);
165
166    const d = DateHelper.getParams( dateObj );
167		return d.y + l2(d.m) + l2(d.d) + l2(d.h) + l2(d.i) + l2(d.s);
168	}
169
170	/** 日付文字列をDateオブジェクトにする
171	 * yyyy-mm-dd(or yyyy/mm/dd or yyyymmdd) to Date obj */
172	static dateStrToDate(str)
173  {
174		if( str === "" || str === null ) {
175			return null;
176    }
177		const mt = str.match(/([\d]{4})(\/|-)?([\d]{1,2})(\/|-)?([\d]{1,2}).*/);
178		return new Date( mt[1], mt[3]-1, mt[5], 0, 0, 0 );
179	}
180
181	/** 日時文字列をDateオブジェクトにする
182	 * yyyy-mm-dd hh:ii:ss(or yyyy/mm/dd  hh:ii:ss or yyyymmddhhiiss) to Date obj */
183	static datetimeStrToDate(str)
184  {
185		if( str === "" || str === null ) {
186			return null;
187    }
188		const mt = str.match(/([\d]{4})(\/|-)?([\d]{1,2})(\/|-)?([\d]{1,2}).?([\d]{1,2}):?([\d]{1,2}):?([\d]{1,2}).*/);
189		return new Date(mt[1], mt[3]-1, mt[5], mt[6], mt[7], mt[8]);
190	}
191}

Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.