PageSourceSearch

https://www.thomas-lurz.de/_/scripts/rgElements/src/Date/Date.js

js thomas-lurz.de collected 2026-10-02 12:54:33 UTC 7,528 bytes, 197 lines download raw bytes

1export function parseDateString(datestring) {
2    if(!datestring){
3        return new Date();
4    }
5    const tstampsyntax = [
6        /^(\d{4})-(\d{2})-(\d{2}) (\d{2}):(\d{2}):(\d{2})$/,
7        // YYYY-MM-DD HH:ii:SS  (DATETIME)
8        /^(\d{4})-(\d{2})-(\d{2}) (\d{2}):(\d{2})$/,
9        // YYYY-MM-DD HH:ii  (DATETIME)
10        /^(\d{4})-(\d{2})-(\d{2})$/,
11        // YYYY-MM-DD (DATE)
12        /^(\d{4})(\d{2})(\d{2})(\d{2})(\d{2})(\d{2})$/,
13        // YYYYMMDDHHIISS (TIMESTAMP)
14        /^(\d{1,2})[-.,: ](\d{1,2})[-.,: ](\d{2,4})[-.,: ](\d{1,2})[-.,: ](\d{1,2})[-.,: ](\d{1,2})$/,
15        // TT,MM,YYYY,HH,mm
16        /^(\d{1,2})[-.,: ](\d{1,2})[-.,: ](\d{2,4})[-.,: ](\d{1,2})[-.,: ](\d{1,2})$/,
17        // TT,MM,YYYY,HH,mm
18        /^(\d{1,2})[-.,: ](\d{1,2})[-.,: ](\d{2,4})$/,
19        // D,M,YY oder DD,MM,YYYY
20        /^(\d{2})(\d{2})(\d{2,4})$/,
21        // TTMMYY oder TTMMYYYY
22        /^(\d{4})-(\d{2})-(\d{2})[\s*]?[A-Za-z]?(\d{2}):(\d{2}):(\d{2})(.*)$/
23        //YYYY-MM-DD[A-Z]HH:ii:SS 2012-04-03T10:22:23+02:00
24    ];
25    const tstampvalue = ['123456', '12345', '123', '123456', '321456', '32145', '321', '321', '123456'];
26    let result = new Date();
27    tstampsyntax.every((regex, index) => {
28            const matches = datestring.match(regex);
29            if(matches) {
30                let dateparts = [];
31                for(let i = 0; i < tstampvalue[index].length; i++) {
32                    dateparts[tstampvalue[index].substring(i, i + 1)] = parseInt(matches[(i + 1)]);
33                }
34                if(dateparts[1] < 70) {
35                    dateparts[1] += 2000;
36                }
37                if(dateparts[1] < 100) {
38                    dateparts[1] += 1900;
39                }
40                dateparts[1] = dateparts[1] ?? 0;
41                dateparts[2] = dateparts[2] ?? 0;
42                dateparts[3] = dateparts[3] ?? 0;
43                dateparts[4] = dateparts[4] ?? 0;
44                dateparts[5] = dateparts[5] ?? 0;
45                dateparts[6] = dateparts[6] ?? 0;
46                result = new Date(dateparts[1], dateparts[2] - 1, dateparts[3], dateparts[4], dateparts[5], dateparts[6]);
47                return false;
48            }
49            return true;
50        }
51    )
52    return result;
53}
54
55/*
56Format  Output      Description
57------  ----------  --------------------------------------------------------------
58x  d      10         Day of the month, 2 digits with leading zeros
59   D      Wed        A textual representation of a day, three letters
60   j      10         Day of the month without leading zeros
61   l      Wednesday  A full textual representation of the day of the week
62   S      th         English ordinal day of month suffix, 2 chars (use with j)
63   w      3          Numeric representation of the day of the week
64   z      9          The julian date, or day of the year (0-365)
65   W      01         ISO-8601 2-digit week number of year, weeks starting on Monday (00-52)
66   F      January    A full textual representation of the month
67   m      01         Numeric representation of a month, with leading zeros
68   M      Jan        Month name abbreviation, three letters
69   n      1          Numeric representation of a month, without leading zeros
70   t      31         Number of days in the given month
71   L      0          Whether it's a leap year (1 if it is a leap year, else 0)
72   Y      2007       A full numeric representation of a year, 4 digits
73   y      07         A two digit representation of a year
74   a      pm         Lowercase Ante meridiem and Post meridiem
75   A      PM         Uppercase Ante meridiem and Post meridiem
76   g      3          12-hour format of an hour without leading zeros
77   G      15         24-hour format of an hour without leading zeros
78   h      03         12-hour format of an hour with leading zeros
79   H      15         24-hour format of an hour with leading zeros
80   i      05         Minutes with leading zeros
81   s      01         Seconds, with leading zeros
82   O      -0600      Difference to Greenwich time (GMT) in hours
83   T      CST        Timezone setting of the machine running the code
84   Z      -21600     Timezone offset in seconds (negative if west of UTC, positive if east)
85 */
86const dateReplacements = {
87    'd' : function(dateString, replacemnt) {
88        let day = this.getDate();
89        day = day < 10 ? '0' + day : String(day);
90        dateString = dateString.replace(replacemnt, day);
91        return dateString;
92    },
93    'D' : function(dateString, replacemnt) {
94        return dateString;
95    },
96    'j' : function(dateString, replacemnt) {
97        return dateString;
98    },
99    'l' : function(dateString, replacemnt) {
100        return dateString;
101    },
102    'S' : function(dateString, replacemnt) {
103        return dateString;
104    },
105    'w' : function(dateString, replacemnt) {
106        return dateString;
107    },
108    'z' : function(dateString, replacemnt) {
109        return dateString;
110    },
111    'W' : function(dateString, replacemnt) {
112        return dateString;
113    },
114    'F' : function(dateString, replacemnt) {
115        return dateString;
116    },
117    'm' : function(dateString, replacemnt) {
118        let month = this.getMonth() + 1;
119        month = month < 10 ? '0' + month : String(month);
120        dateString = dateString.replace(replacemnt, month);
121        return dateString;
122    },
123    'M' : function(dateString, replacemnt) {
124        return dateString;
125    },
126    'n' : function(dateString, replacemnt) {
127        return dateString;
128    },
129    't' : function(dateString, replacemnt) {
130        return dateString;
131    },
132    'L' : function(dateString, replacemnt) {
133        return dateString;
134    },
135    'Y' : function(dateString, replacemnt) {
136        dateString = dateString.replace(replacemnt, this.getFullYear())
137        return dateString;
138    },
139    'y' : function(dateString, replacemnt) {
140        return dateString;
141    },
142    'a' : function(dateString, replacemnt) {
143        return dateString;
144    },
145    'A' : function(dateString, replacemnt) {
146        return dateString;
147    },
148    'g' : function(dateString, replacemnt) {
149        return dateString;
150    },
151    'G' : function(dateString, replacemnt) {
152        return dateString;
153    },
154    'h' : function(dateString, replacemnt) {
155        return dateString;
156    },
157    'H' : function(dateString, replacemnt) {
158        let hours = this.getHours();
159        hours = hours < 10 ? '0' + hours : String(hours);
160        dateString = dateString.replace(replacemnt, hours);
161        return dateString;
162    },
163    'i' : function(dateString, replacemnt) {
164        let minutes = this.getMinutes();
165        minutes = minutes < 10 ? '0' + minutes : String(minutes);
166        dateString = dateString.replace(replacemnt, minutes);
167        return dateString;
168    },
169    's' : function(dateString, replacemnt) {
170        let seconds = this.getSeconds();
171        seconds = seconds < 10 ? '0' + seconds : String(seconds);
172        dateString = dateString.replace(replacemnt, seconds);
173        return dateString;
174    },
175    'O' : function(dateString, replacemnt) {
176        return dateString;
177    },
178    'T' : function(dateString, replacemnt) {
179        return dateString;
180    },
181    'Z' : function(dateString, replacemnt) {
182        return dateString;
183    },
184};
185export function format(date) {
186    if(isNaN(this)) {
187        return '';
188    }
189    for (const [replacement, fnc] of Object.entries(dateReplacements)) {
190        date = fnc.call(this, date, replacement);
191    }
192    return date;
193}
194
195export function clearTime() {
196    return new Date(this.toDateString());
197}

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.