@@ -191,6 +191,106 @@ class HarfizerConverter {
191191 : negativeWord ;
192192 return ( ( isNegative ? negPrefix : "" ) + wordParts . join ( separator ) + fractionPart ) ;
193193 }
194+ /**
195+ * Converts a date string (Solar/Jalali or Gregorian) to its word representation.
196+ *
197+ * @param {string } dateStr - The date string in the format 'YYYY/MM/DD' or 'YYYY-MM-DD'.
198+ * @param {'jalali' | 'gregorian' } [calendar='jalali'] - The calendar type, either 'jalali' or 'gregorian'.
199+ * @returns {string } The word representation of the date.
200+ * @throws {Error } Throws an error if the date format is invalid or the month is out of range.
201+ */
202+ convertDateToWords ( dateStr , calendar = "jalali" ) {
203+ // Split the date string by '/' or '-' and validate the format.
204+ const parts = dateStr . split ( / [ - \/ ] / ) ;
205+ if ( parts . length !== 3 ) {
206+ throw new Error ( "Invalid date format. Expected format 'YYYY/MM/DD' or 'YYYY-MM-DD'." ) ;
207+ }
208+ const [ yearStr , monthStr , dayStr ] = parts ;
209+ // Parse month as a number and validate it.
210+ const monthNum = parseInt ( monthStr , 10 ) ;
211+ if ( isNaN ( monthNum ) || monthNum < 1 || monthNum > 12 ) {
212+ throw new Error ( "Invalid month in date." ) ;
213+ }
214+ // Define month names for Jalali and Gregorian calendars.
215+ const jalaliMonths = [
216+ "فروردین" ,
217+ "اردیبهشت" ,
218+ "خرداد" ,
219+ "تیر" ,
220+ "مرداد" ,
221+ "شهریور" ,
222+ "مهر" ,
223+ "آبان" ,
224+ "آذر" ,
225+ "دی" ,
226+ "بهمن" ,
227+ "اسفند" ,
228+ ] ;
229+ const gregorianMonths = [
230+ "ژانویه" ,
231+ "فوریه" ,
232+ "مارس" ,
233+ "آوریل" ,
234+ "مه" ,
235+ "ژوئن" ,
236+ "ژوئیه" ,
237+ "اوت" ,
238+ "سپتامبر" ,
239+ "اکتبر" ,
240+ "نوامبر" ,
241+ "دسامبر" ,
242+ ] ;
243+ // Get the month name based on the calendar type.
244+ const monthName = calendar === "gregorian"
245+ ? gregorianMonths [ monthNum - 1 ]
246+ : jalaliMonths [ monthNum - 1 ] ;
247+ // Convert the day and year to words using the convert method.
248+ const dayWords = this . convert ( dayStr ) ;
249+ const yearWords = this . convert ( yearStr ) ;
250+ // Construct and return the final word representation of the date.
251+ return `${ dayWords } ${ monthName } ${ yearWords } ` ;
252+ }
253+ /**
254+ * Converts a digital time string (HH:mm) to its Persian word representation.
255+ *
256+ * @param {string } timeStr - The time string in "HH:mm" format.
257+ * @returns {string } The word representation of the time.
258+ * @throws {Error } Throws an error if the time format is invalid or if time values are out of range.
259+ */
260+ convertTimeToWords ( timeStr ) {
261+ // Split the time string using colon (":") as the separator.
262+ const parts = timeStr . split ( ":" ) ;
263+ if ( parts . length !== 2 ) {
264+ throw new Error ( "Invalid time format. Expected format 'HH:mm'." ) ;
265+ }
266+ const [ hourStr , minuteStr ] = parts ;
267+ // Parse hours and minutes.
268+ const hour = parseInt ( hourStr , 10 ) ;
269+ const minute = parseInt ( minuteStr , 10 ) ;
270+ if ( isNaN ( hour ) || isNaN ( minute ) ) {
271+ throw new Error ( "Invalid time format. Hours and minutes should be numbers." ) ;
272+ }
273+ // Validate hour and minute ranges.
274+ if ( hour < 0 || hour > 23 ) {
275+ throw new Error ( "Invalid hour value. Hour should be between 0 and 23." ) ;
276+ }
277+ if ( minute < 0 || minute > 59 ) {
278+ throw new Error ( "Invalid minute value. Minute should be between 0 and 59." ) ;
279+ }
280+ // Retrieve the custom time prefix from configuration or use the default "ساعت".
281+ const timePrefix = this . config . customTimePrefix ?? "ساعت" ;
282+ // Convert the hour and minute values to their word representations.
283+ const hourWords = this . convert ( hour ) ;
284+ const minuteWords = this . convert ( minute ) ;
285+ // Construct the final time string in words.
286+ // If minutes are zero, return only the hour part.
287+ if ( minute === 0 ) {
288+ return `${ timePrefix } ${ hourWords } ` ;
289+ }
290+ else {
291+ return `${ timePrefix } ${ hourWords } و ${ minuteWords } دقیقه` ;
292+ }
293+ }
194294 /**
195295 * Returns the default separator from the configuration or default constant.
196296 *
0 commit comments