diff --git a/package-lock.json b/package-lock.json index 2e814704..cf7e0dcc 100644 --- a/package-lock.json +++ b/package-lock.json @@ -17,6 +17,7 @@ "@uiw/codemirror-theme-nord": "^4.21.21", "@uiw/react-codemirror": "^4.21.21", "bootstrap": "^5.3.2", + "date-fns": "^3.6.0", "eslint-config-next": "^14.0.2", "fast-xml-parser": "^4.3.3", "firebase": "^10.6.0", @@ -1634,6 +1635,22 @@ "url": "https://mockoon.com/sponsor-us/" } }, + "node_modules/@mockoon/cli/node_modules/date-fns": { + "version": "2.30.0", + "resolved": "https://registry.npmjs.org/date-fns/-/date-fns-2.30.0.tgz", + "integrity": "sha512-fnULvOpxnC5/Vg3NCiWelDsLiUc9bRwAPs/+LfTLNvetFCtCTN+yQz15C/fs4AwX1R9K5GLtLfn8QW+dWisaAw==", + "dev": true, + "dependencies": { + "@babel/runtime": "^7.21.0" + }, + "engines": { + "node": ">=0.11" + }, + "funding": { + "type": "opencollective", + "url": "https://opencollective.com/date-fns" + } + }, "node_modules/@mockoon/cli/node_modules/jsonpath-plus": { "version": "7.2.0", "resolved": "https://registry.npmjs.org/jsonpath-plus/-/jsonpath-plus-7.2.0.tgz", @@ -5398,6 +5415,22 @@ "url": "https://github.com/open-cli-tools/concurrently?sponsor=1" } }, + "node_modules/concurrently/node_modules/date-fns": { + "version": "2.30.0", + "resolved": "https://registry.npmjs.org/date-fns/-/date-fns-2.30.0.tgz", + "integrity": "sha512-fnULvOpxnC5/Vg3NCiWelDsLiUc9bRwAPs/+LfTLNvetFCtCTN+yQz15C/fs4AwX1R9K5GLtLfn8QW+dWisaAw==", + "dev": true, + "dependencies": { + "@babel/runtime": "^7.21.0" + }, + "engines": { + "node": ">=0.11" + }, + "funding": { + "type": "opencollective", + "url": "https://opencollective.com/date-fns" + } + }, "node_modules/config-chain": { "version": "1.1.13", "resolved": "https://registry.npmjs.org/config-chain/-/config-chain-1.1.13.tgz", @@ -5767,19 +5800,12 @@ } }, "node_modules/date-fns": { - "version": "2.30.0", - "resolved": "https://registry.npmjs.org/date-fns/-/date-fns-2.30.0.tgz", - "integrity": "sha512-fnULvOpxnC5/Vg3NCiWelDsLiUc9bRwAPs/+LfTLNvetFCtCTN+yQz15C/fs4AwX1R9K5GLtLfn8QW+dWisaAw==", - "dev": true, - "dependencies": { - "@babel/runtime": "^7.21.0" - }, - "engines": { - "node": ">=0.11" - }, + "version": "3.6.0", + "resolved": "https://registry.npmjs.org/date-fns/-/date-fns-3.6.0.tgz", + "integrity": "sha512-fRHTG8g/Gif+kSh50gaGEdToemgfj74aRX3swtiouboip5JDLAyDE9F11nHMIcvOaXeOC6D7SpNhi7uFyB7Uww==", "funding": { - "type": "opencollective", - "url": "https://opencollective.com/date-fns" + "type": "github", + "url": "https://github.com/sponsors/kossnocorp" } }, "node_modules/debug": { @@ -17410,6 +17436,15 @@ "xml-js": "1.6.11" } }, + "date-fns": { + "version": "2.30.0", + "resolved": "https://registry.npmjs.org/date-fns/-/date-fns-2.30.0.tgz", + "integrity": "sha512-fnULvOpxnC5/Vg3NCiWelDsLiUc9bRwAPs/+LfTLNvetFCtCTN+yQz15C/fs4AwX1R9K5GLtLfn8QW+dWisaAw==", + "dev": true, + "requires": { + "@babel/runtime": "^7.21.0" + } + }, "jsonpath-plus": { "version": "7.2.0", "resolved": "https://registry.npmjs.org/jsonpath-plus/-/jsonpath-plus-7.2.0.tgz", @@ -20480,6 +20515,17 @@ "supports-color": "^8.1.1", "tree-kill": "^1.2.2", "yargs": "^17.7.2" + }, + "dependencies": { + "date-fns": { + "version": "2.30.0", + "resolved": "https://registry.npmjs.org/date-fns/-/date-fns-2.30.0.tgz", + "integrity": "sha512-fnULvOpxnC5/Vg3NCiWelDsLiUc9bRwAPs/+LfTLNvetFCtCTN+yQz15C/fs4AwX1R9K5GLtLfn8QW+dWisaAw==", + "dev": true, + "requires": { + "@babel/runtime": "^7.21.0" + } + } } }, "config-chain": { @@ -20786,13 +20832,9 @@ "dev": true }, "date-fns": { - "version": "2.30.0", - "resolved": "https://registry.npmjs.org/date-fns/-/date-fns-2.30.0.tgz", - "integrity": "sha512-fnULvOpxnC5/Vg3NCiWelDsLiUc9bRwAPs/+LfTLNvetFCtCTN+yQz15C/fs4AwX1R9K5GLtLfn8QW+dWisaAw==", - "dev": true, - "requires": { - "@babel/runtime": "^7.21.0" - } + "version": "3.6.0", + "resolved": "https://registry.npmjs.org/date-fns/-/date-fns-3.6.0.tgz", + "integrity": "sha512-fRHTG8g/Gif+kSh50gaGEdToemgfj74aRX3swtiouboip5JDLAyDE9F11nHMIcvOaXeOC6D7SpNhi7uFyB7Uww==" }, "debug": { "version": "4.3.4", diff --git a/package.json b/package.json index 389ef984..9252dc6d 100644 --- a/package.json +++ b/package.json @@ -43,6 +43,7 @@ "@uiw/codemirror-theme-nord": "^4.21.21", "@uiw/react-codemirror": "^4.21.21", "bootstrap": "^5.3.2", + "date-fns": "^3.6.0", "eslint-config-next": "^14.0.2", "fast-xml-parser": "^4.3.3", "firebase": "^10.6.0", diff --git a/pages/tools/date-unix-timestamp-converter-formatter.tsx b/pages/tools/date-unix-timestamp-converter-formatter.tsx new file mode 100644 index 00000000..f55dc602 --- /dev/null +++ b/pages/tools/date-unix-timestamp-converter-formatter.tsx @@ -0,0 +1,343 @@ +import { + format, + setDate, + setHours, + setMinutes, + setMonth, + setSeconds, + setYear +} from 'date-fns'; +import { FunctionComponent, useState } from 'react'; +import Hero from '../../components/hero'; +import Meta from '../../components/meta'; +import Layout from '../../layout/layout'; + +const DateConverter: FunctionComponent = function () { + const [currentDate, setCurrentDate] = useState(new Date()); + const [customFormat, setCustomFormat] = useState('yyyy-MM-dd'); + + const formatDate = (date: Date, pattern: string) => { + try { + return format(date, pattern, { + useAdditionalDayOfYearTokens: true, + useAdditionalWeekYearTokens: true + }); + } catch (error) {} + }; + + return ( + + + +
+
+
+
+
+
+ + { + try { + setCurrentDate( + setYear(currentDate, parseInt(event.target.value)) + ); + } catch (error) {} + }} + /> +
+
+ + { + try { + setCurrentDate( + setMonth(currentDate, parseInt(event.target.value)) + ); + } catch (error) {} + }} + /> +
+
+ + { + try { + setCurrentDate( + setDate(currentDate, parseInt(event.target.value)) + ); + } catch (error) {} + }} + /> +
+
+ + { + try { + setCurrentDate( + setHours(currentDate, parseInt(event.target.value)) + ); + } catch (error) {} + }} + /> +
+
+ + { + try { + setCurrentDate( + setMinutes(currentDate, parseInt(event.target.value)) + ); + } catch (error) {} + }} + /> +
+
+ + { + try { + setCurrentDate( + setSeconds(currentDate, parseInt(event.target.value)) + ); + } catch (error) {} + }} + /> +
+
+
+ + { + try { + setCurrentDate( + new Date(parseInt(event.target.value) * 1000) + ); + } catch (error) {} + }} + /> +
+
+ + { + try { + setCurrentDate(new Date(parseInt(event.target.value))); + } catch (error) {} + }} + /> +
+
+
+
+ + { + try { + setCustomFormat(event.target.value); + } catch (error) {} + }} + /> +

+ Result:{' '} + {customFormat ? formatDate(currentDate, customFormat) : ''} +

+
+ + + + + + + + + + + + + + + + + + + + + + + + + + + +
FormatResult
+ + ISO 8601, RFC 3339 + + + {formatDate(currentDate, "yyyy-MM-dd'T'HH:mm:ss.SSSxxx")} +
+ + date-fns format:{' '} + + yyyy-MM-dd'T'HH:mm:ss.SSSxxx + + +
+ + RFC 822/2822 + + + {formatDate(currentDate, 'EEE, dd MMM yyyy HH:mm:ss xx')} +
+ + date-fns format:{' '} + + EEE, dd MMM yyyy HH:mm:ss xx + + +
Unix timestamp (s) + {Math.floor(currentDate.getTime() / 1000)} +
+ + date-fns format:{' '} + + t + + +
Unix timestamp (ms) + {currentDate.getTime()} +
+ + date-fns format:{' '} + + T + + +
+
+
+
+
+ +
+
+
+
+

About this tool

+

+ This tool allows you to convert a date to a unix timestamp + (seconds or milliseconds) and vice versa, and view the date in + different formats (ISO 8601, RFC 3339, RFC 822/2822). You can + also format the date in a custom format using the{' '} + + date-fns library format + + . +

+

About Unix timestamps

+

+ A Unix timestamp (or epoch time) is the number of seconds or + milliseconds that have elapsed since 00:00:00 Coordinated + Universal Time (UTC), Thursday, 1 January 1970. It is used in + many programming languages and systems to represent dates and + times. +
+
+ Unix timestamps are often used in APIs to represent dates and + times because they are easy to work with and can be converted to + any time zone. +

+

About date-fns library

+

+ date-fns is a modern JavaScript date utility + library that provides comprehensive, yet simple and consistent + toolset for manipulating JavaScript dates in a browser or + Node.js environment. +
+
+ It is used in this tool to format dates and unix timestamps in + different formats using predefined patterns. You can find the + list of available patterns in the{' '} + + date-fns documentation + + . +

+
+
+
+
+
+ ); +}; + +export default DateConverter; diff --git a/pages/tools/index.tsx b/pages/tools/index.tsx index 39963ac7..e4e5bac8 100644 --- a/pages/tools/index.tsx +++ b/pages/tools/index.tsx @@ -59,6 +59,19 @@ const tools = [ 'Count the number of characters, words, and lines in your text', links: [{ src: '/tools/string-length-word-counter/', text: 'Count' }], imageSrc: '/images/illustrations/string-length-word-counter.svg' + }, + { + title: 'Date and unix timestamp converter and formatter', + description: + 'Convert and format dates and unix timestamps online using date-fns library', + links: [ + { + src: '/tools/date-unix-timestamp-converter-formatter/', + text: 'Convert and format' + } + ], + imageSrc: + '/images/illustrations/date-unix-timestamp-converter-formatter.svg' } ]; diff --git a/public/images/illustrations/date-unix-timestamp-converter-formatter.svg b/public/images/illustrations/date-unix-timestamp-converter-formatter.svg new file mode 100644 index 00000000..7a98aec3 --- /dev/null +++ b/public/images/illustrations/date-unix-timestamp-converter-formatter.svg @@ -0,0 +1,22 @@ + + + + + + + + + + + + + + + + + + + + + +