Skip to content

Commit 07329d3

Browse files
authored
add convertToUnixSeconds date utility function (#77)
1 parent 485337f commit 07329d3

File tree

1 file changed

+50
-5
lines changed

1 file changed

+50
-5
lines changed

public/data/javascript.json

Lines changed: 50 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -246,7 +246,7 @@
246246
"tags": ["string", "case", "camelCase"],
247247
"author": "aumirza"
248248
},
249-
{
249+
{
250250
"title": "Convert String to Title Case",
251251
"description": "Converts a given string into Title Case.",
252252
"code": [
@@ -260,7 +260,7 @@
260260
"tags": ["string", "case", "titleCase"],
261261
"author": "aumirza"
262262
},
263-
{
263+
{
264264
"title": "Convert String to Pascal Case",
265265
"description": "Converts a given string into Pascal Case.",
266266
"code": [
@@ -274,7 +274,7 @@
274274
"tags": ["string", "case", "pascalCase"],
275275
"author": "aumirza"
276276
},
277-
{
277+
{
278278
"title": "Convert String to Param Case",
279279
"description": "Converts a given string into param-case.",
280280
"code": [
@@ -786,6 +786,51 @@
786786
],
787787
"tags": ["javascript", "date", "day-of-year", "utility"],
788788
"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"
789834
}
790835
]
791836
},
@@ -1315,7 +1360,7 @@
13151360
}
13161361
]
13171362
},
1318-
{
1363+
{
13191364
"categoryName": "Regular expression",
13201365
"snippets": [
13211366
{
@@ -1353,7 +1398,7 @@
13531398
" });",
13541399
"}"
13551400
],
1356-
"tags": ["javascript","regex"],
1401+
"tags": ["javascript", "regex"],
13571402
"author": "aumirza"
13581403
}
13591404
]

0 commit comments

Comments
 (0)