11/**
22 * Get a formatted date string based on the given timestamp.
3- *
3+ * ```js
4+ * const result = getFormattedDate(1702292155654);
5+ * console.log(result);
6+ * const date = new Date(r);
7+ * console.log(date);
8+ * ```
49 * @param {number } timestamp - The timestamp to convert to a formatted date.
510 * @param {Object } options - Options for formatting the date.
611 * @param {string } [options.lang="de-DE"] - The language for the formatted date.
712 * @returns {string } The formatted date string.
813 */
9- function getFormattedDate ( timestamp , { lang = "de-DE" } = { } ) {
14+ export function getFormattedDate ( timestamp , { lang = "de-DE" } = { } ) {
1015 const date = new Date ( timestamp ) ;
1116 const options /** @type {Intl.DateTimeFormatOptions } */ = {
1217 year : "numeric" ,
@@ -20,11 +25,6 @@ function getFormattedDate(timestamp, { lang = "de-DE" } = {}) {
2025 return formattedDate ;
2126}
2227
23- const r = getFormattedDate ( 1702292155654 ) ;
24- console . log ( r ) ;
25- const date = new Date ( r ) ;
26- console . log ( date ) ;
27-
2828/**
2929 * Converts a formatted date string to a timestamp.
3030 *
@@ -64,7 +64,14 @@ console.log(date);
6464// console.log(timestamp);
6565/**
6666 * Converts a formatted date string to a timestamp using the Date constructor.
67- *
67+ * ```js
68+ * const timestamp = convertFormattedDateToTimestamp(
69+ * "11. Dezember 2023 um 11:55",
70+ * { lang: "de-DE" },
71+ * );
72+ * console.log(timestamp);
73+ * console.log(getFormattedDate(timestamp));
74+ * ```
6875 * @param {string } formattedDate - The formatted date string.
6976 * @param {Object } options - Options for the conversion.
7077 * @param {string } [options.lang="de-DE"] - The language used in the original formatting.
@@ -93,17 +100,12 @@ function convertFormattedDateToTimestamp(
93100
94101 // Split the formatted date string into parts
95102 const [ day , month , year , , time ] = formattedDate . split ( " " ) ;
96- console . log ( formattedDate . split ( " " ) ) ;
97-
98103 // Extract numeric values from parts
99104 const dayNumeric = parseInt ( day , 10 ) ;
100105 const monthNumeric = monthNames [ month ] ;
101106 const yearNumeric = parseInt ( year , 10 ) ;
102107 // Extract hours and minutes from time
103108 const [ hours , minutes ] = time . split ( ":" ) . map ( ( part ) => parseInt ( part , 10 ) ) ;
104- console . log ( "time:" , time ) ;
105- console . log ( dayNumeric , monthNumeric , yearNumeric , hours , minutes ) ;
106-
107109 // Create a new Date object
108110 const date = new Date (
109111 yearNumeric ,
@@ -112,20 +114,9 @@ function convertFormattedDateToTimestamp(
112114 hours ,
113115 minutes ,
114116 ) ;
115- console . log ( "aaaaa Date:" , date ) ;
116-
117117 const timestamp = date . getTime ( ) ;
118118 return timestamp ;
119119 } catch ( error ) {
120- console . error ( `Error parsing date: ${ formattedDate } ` ) ;
121- return null ;
120+ throw error ;
122121 }
123122}
124-
125- // Example usage:
126- const timestamp = convertFormattedDateToTimestamp (
127- "11. Dezember 2023 um 11:55" ,
128- { lang : "de-DE" } ,
129- ) ;
130- console . log ( timestamp ) ;
131- console . log ( getFormattedDate ( timestamp ) ) ;
0 commit comments