|
246 | 246 | "tags": ["string", "case", "camelCase"],
|
247 | 247 | "author": "aumirza"
|
248 | 248 | },
|
249 |
| - { |
| 249 | + { |
250 | 250 | "title": "Convert String to Title Case",
|
251 | 251 | "description": "Converts a given string into Title Case.",
|
252 | 252 | "code": [
|
|
260 | 260 | "tags": ["string", "case", "titleCase"],
|
261 | 261 | "author": "aumirza"
|
262 | 262 | },
|
263 |
| - { |
| 263 | + { |
264 | 264 | "title": "Convert String to Pascal Case",
|
265 | 265 | "description": "Converts a given string into Pascal Case.",
|
266 | 266 | "code": [
|
|
274 | 274 | "tags": ["string", "case", "pascalCase"],
|
275 | 275 | "author": "aumirza"
|
276 | 276 | },
|
277 |
| - { |
| 277 | + { |
278 | 278 | "title": "Convert String to Param Case",
|
279 | 279 | "description": "Converts a given string into param-case.",
|
280 | 280 | "code": [
|
|
786 | 786 | ],
|
787 | 787 | "tags": ["javascript", "date", "day-of-year", "utility"],
|
788 | 788 | "author": "axorax"
|
| 789 | + }, |
| 790 | + { |
| 791 | + "title": "Convert to Unix Timestamp", |
| 792 | + "description": "Converts a date to a Unix timestamp in seconds.", |
| 793 | + "code": [ |
| 794 | + "/**", |
| 795 | + " * Converts a date string or Date object to Unix timestamp in seconds.", |
| 796 | + " *", |
| 797 | + " * @param {string|Date} input - A valid date string or Date object.", |
| 798 | + " * @returns {number} - The Unix timestamp in seconds.", |
| 799 | + " * @throws {Error} - Throws an error if the input is invalid.", |
| 800 | + " */", |
| 801 | + "function convertToUnixSeconds(input) {", |
| 802 | + " if (typeof input === 'string') {", |
| 803 | + " if (!input.trim()) {", |
| 804 | + " throw new Error('Date string cannot be empty or whitespace');", |
| 805 | + " }", |
| 806 | + " } else if (!input) {", |
| 807 | + " throw new Error('Input is required');", |
| 808 | + " }", |
| 809 | + "", |
| 810 | + " let date;", |
| 811 | + "", |
| 812 | + " if (typeof input === 'string') {", |
| 813 | + " date = new Date(input);", |
| 814 | + " } else if (input instanceof Date) {", |
| 815 | + " date = input;", |
| 816 | + " } else {", |
| 817 | + " throw new Error('Input must be a valid date string or Date object');", |
| 818 | + " }", |
| 819 | + "", |
| 820 | + " if (isNaN(date.getTime())) {", |
| 821 | + " throw new Error('Invalid date provided');", |
| 822 | + " }", |
| 823 | + "", |
| 824 | + " return Math.floor(date.getTime() / 1000);", |
| 825 | + "}", |
| 826 | + "", |
| 827 | + "// Usage", |
| 828 | + "console.log(convertToUnixSeconds('2025-01-01T12:00:00Z')); // 1735732800", |
| 829 | + "console.log(convertToUnixSeconds(new Date('2025-01-01T12:00:00Z'))); // 1735732800", |
| 830 | + "console.log(convertToUnixSeconds(new Date())); //Current Unix timestamp in seconds (varies depending on execution time)" |
| 831 | + ], |
| 832 | + "tags": ["javascript", "date", "unix", "timestamp", "utility"], |
| 833 | + "author": "Yugveer06" |
789 | 834 | }
|
790 | 835 | ]
|
791 | 836 | },
|
|
1315 | 1360 | }
|
1316 | 1361 | ]
|
1317 | 1362 | },
|
1318 |
| - { |
| 1363 | + { |
1319 | 1364 | "categoryName": "Regular expression",
|
1320 | 1365 | "snippets": [
|
1321 | 1366 | {
|
|
1353 | 1398 | " });",
|
1354 | 1399 | "}"
|
1355 | 1400 | ],
|
1356 |
| - "tags": ["javascript","regex"], |
| 1401 | + "tags": ["javascript", "regex"], |
1357 | 1402 | "author": "aumirza"
|
1358 | 1403 | }
|
1359 | 1404 | ]
|
|
0 commit comments