Search code examples
viewtelegraminstant

How to use @datetime function in telegram instant view?


I am trying to set up a telegram Instant View. I am facing problem with the function datetime @datetime, I've looked at the official documentation.

I have the following date Jul 19, 2018 at 2:25pm. In case we are on the same year of the date, the string won't contain the year ex: Jul 19 at 2:25pm means 19 July of this year. How can I deal with the missing year?

This is my code so far.

@datetime(-2, "en-US", "LLL d 'at' k:mm"): "Jan 25 at 2:44pm"
published_date: $@

#manage the current year case
@datetime(0, "en-US", "LLL d, YYYY 'at' k:mma"): "Jan 25, 2018 at 2:44pm"
published_date: $@

As of now the missing year is not properly managed. In this way the year is always 1970.


Solution

  • The algorithm is following:

    1. Try to parse the date with YYYY inside
    2. If failing, the $pubslished_date will contain 0 or some garbage (try to @debug it). So you can use something like @if_not( $published_date ) { ... }, where you can try to parse the date without YYYY. Don't forget to force redefine the variable with published_date!: ….

    If that won't work, try to play with conditional binding: pubslihed_date?: …. It has the same logic. (Just to put a question mark ? in the second binding in your current code).