Time or Date Format of Javascript Transformation

Hello everybody,

I skipped all initial information because I think it doesn’t affect my question.

I know how to format DateTime Items the way I want them to be:
DateTime ItemName "Date and Time [%1$td.%1$tm.%1$ty, %1$tH:%1$tM]"

Secondly it’s quite easy to use JavaScript code for calculation of Item values:
Number ItemName "State since [JS(sek_zu_hms.js):%s]"

But is there any possibility to combine these two things, just like that:
DateTime ItemName "Date and Time [JS(sek_zu_hms.js):%1$td.%1$tm.%1$ty, %1$tH:%1$tM]"

I couldn’t find a solution in the community and appreciate your help!

Thanks a lot,
beinaendi

Would a profile follow do what you need?

Hi H102,

no, I don’t think so. What I get from Item binding is an invalid timestamp like “2020-10-13 17:15pm”. JavaScript transforms this to a valid DateTime format. Of course I can return a preformatted String that represents what I want. But in my opinion returning a DateTime and formatting it in the Item label would be more correct.

This is not possible. You have either the transformation or the formatting, not both. You would need to modify your JS transformation to return exactly the string you want.

Well then it’s solved. That’s what I did till now but I was looking for a better solution.

I read JSON data which is formatted the wrong in all it’s values. Sometimes I want it to be shown including seconds, sometimes without. So a JavaScript transformation to DateTime and formatting the Item would be ideal.

But it’s worth the effort to use different JavaScript transformations instead.

Thanks rlkoshak

Just to provide a little bit of background here, the reason why it won’t work is that the transformation always returns a String and the result of the transform gets passed to the label formatter (i.e. the %s). So when you have a transformation, the sorts of label manipulation that is available to you is limited to what you can do with a String. %1td et. al. only apply to DateTime objects.

Ok, that’s what I thought. But strange thing is that Item declaration accepts type DateTime though. Any explanation for this?

When you don’t have a transformation in the label (e.g. [%1$td.%1$tm.%1$ty, %1$tH:%1$tM]) the DateTimeType is what gets passed to the formatter so it can take apart the Object and build the string in the format specified.

When you have a transformation in the label (e.g. [JS(sek_zu_hms.js):%s]) the DateTimeType get’s lost. First the DateTimeType gets passed to the transformation which does it’s transfomration and then returns a String and that String get’s passed to the label formatter. It’s now a String so %1$td is meaningless in that case.

I don’t know what you mean by “Item declaration accepts type DateTime”.

It means that when I use the JavaScript transformation I always get a String so the Item type should be String like
String ItemName "State since [JS(sek_zu_hms.js):%s]"

On the other Hand, without JS transformation and if binding gets a DateTime Format, I can use Item type DateTime like
DateTime ItemName "Date and Time [%1$td.%1$tm.%1$ty, %1$tH:%1$tM]"

The strange this is that an Item declared as DateTime but only with a string value works:
DateTime ItemName "State since [JS(sek_zu_hms.js):%s]"

No, all transforms receive the state as a String in all cases. That’s how transformations work. But you don’t have to throw away the fact that an Item is a DateTime for everything else that uses that Item as a DateTime in OH because of the way that transformations work to show the label.

Exactly, because you are not using a transformation. Therefore the state doesn’t get converted to a String and the %1$td can work.

I already explained why that’s the case. Once you involve a transformation the DateTimeType gets converted to a String and all that you have to work with is a String from that point forward to build the label. %1$td et. al. only work with a DateTimeType. If you use a transformation, you lose the DateTimeType.

When you do not involve a transformation, no conversion occurs and the state remains a DateTimeType so %1$td works.

I’m still not sure if you got me right, but I understood what you mean.

So I tested it once again and that’s my conclusion:

  • DateTime without transformation, use DateTime Item type
  • DateTime with transformation results to a String.
  • If the string can be interpreted as a DateTime, you can use DateTime Item type.
  • If the string cannot be interpreted as a DateTime, you must use String Item type or you get an error.

My question was, why DateTime Item type works with a value transformed to String. Now I know everything about that, thanks a lot!