@@ -210,7 +210,8 @@ const handleCheckbox = (element, prefix = "", suffix = "") => {
210210const handleDateTime = ( element , value , valueType ) => {
211211 const inputType = ( element . getAttribute ( "type" ) || element . type || "" ) . toLowerCase ( ) ;
212212 let date ;
213- if ( value === "$now" ) {
213+
214+ if ( value === "$now" || element . hasAttribute ( "now" ) ) {
214215 date = new Date ( ) ;
215216 } else if ( value instanceof Date ) {
216217 // If it's already a date object, clone it to avoid mutating references
@@ -313,30 +314,15 @@ const handleDateTime = (element, value, valueType) => {
313314 switch ( valueType ) {
314315 case "getDayName" :
315316 const days = [
316- "Sunday" ,
317- "Monday" ,
318- "Tuesday" ,
319- "Wednesday" ,
320- "Thursday" ,
321- "Friday" ,
322- "Saturday" ,
317+ "Sunday" , "Monday" , "Tuesday" , "Wednesday" ,
318+ "Thursday" , "Friday" , "Saturday" ,
323319 ] ;
324320 return days [ date . getDay ( ) ] ;
325321
326322 case "getMonthName" :
327323 const months = [
328- "January" ,
329- "February" ,
330- "March" ,
331- "April" ,
332- "May" ,
333- "June" ,
334- "July" ,
335- "August" ,
336- "September" ,
337- "October" ,
338- "November" ,
339- "December" ,
324+ "January" , "February" , "March" , "April" , "May" , "June" ,
325+ "July" , "August" , "September" , "October" , "November" , "December" ,
340326 ] ;
341327 return months [ date . getMonth ( ) ] ;
342328
@@ -351,6 +337,41 @@ const handleDateTime = (element, value, valueType) => {
351337 hours = hours % 12 || 12 ; // Convert 0 to 12 for midnight
352338 return `${ hours } :${ minutes } ${ ampm } ` ;
353339
340+ case "timeTo" : {
341+ const diffMs = date . getTime ( ) - Date . now ( ) ;
342+ let totalSeconds = Math . floor ( Math . abs ( diffMs ) / 1000 ) ;
343+
344+ // Fetch requested units, defaulting to all if attribute isn't present
345+ const unitsAttr = element . getAttribute ( "time-to-units" ) || "years,months,days,hours,minutes,seconds" ;
346+ const requestedUnits = unitsAttr . split ( ',' ) . map ( u => u . trim ( ) . toLowerCase ( ) ) ;
347+
348+ const result = { } ;
349+
350+ // Using the exact same time approximations as your timeAgo feature
351+ const unitValues = {
352+ years : 31536000 ,
353+ months : 2592000 ,
354+ weeks : 604800 ,
355+ days : 86400 ,
356+ hours : 3600 ,
357+ minutes : 60 ,
358+ seconds : 1
359+ } ;
360+
361+ // The order matters here (largest to smallest) so we "consume" time properly
362+ const order = [ "years" , "months" , "weeks" , "days" , "hours" , "minutes" , "seconds" ] ;
363+
364+ for ( const unit of order ) {
365+ if ( requestedUnits . includes ( unit ) ) {
366+ const val = Math . floor ( totalSeconds / unitValues [ unit ] ) ;
367+ result [ unit ] = val ;
368+ totalSeconds -= val * unitValues [ unit ] ; // subtract the accounted time
369+ }
370+ }
371+
372+ return result ;
373+ }
374+
354375 case "timeAgo" : {
355376 const diffMs = Date . now ( ) - date . getTime ( ) ;
356377 const absSecs = Math . floor ( Math . abs ( diffMs ) / 1000 ) ;
@@ -423,7 +444,6 @@ const handleDateTime = (element, value, valueType) => {
423444 }
424445 }
425446} ;
426-
427447/**
428448 * Processes a <select> HTML element with the "multiple" attribute to retrieve an array of selected option values.
429449 * Each selected value can have a prefix or suffix added to it if specified.
0 commit comments