如何在电报即时视图中使用@datetime函数?

问题描述 投票:0回答:1

我正在尝试设置电报即时视图。我面临着函数datetime @datetime的问题,我看了official documentation

我有以下日期Jul 19, 2018 at 2:25pm。如果我们在该日期的同一年,该字符串将不包含年份:Jul 19 at 2:25pm表示今年7月19日。我怎样才能处理缺失的一年?

到目前为止这是我的代码。

@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: $@

截至目前,缺失的一年没有得到妥善管理。通过这种方式,这一年总是1970年。

view telegram instant
1个回答
0
投票

该算法如下:

  1. 尝试用里面的YYYY解析日期
  2. 如果失败,$pubslished_date将包含0或一些垃圾(尝试@debug它)。所以你可以使用像@if_not( $published_date ) { ... }这样的东西,在那里你可以尝试解析没有YYYY的日期。不要忘记用published_date!: …强制重新定义变量。

如果这不起作用,请尝试使用条件绑定:pubslihed_date?: …。它有相同的逻辑。 (只是在当前代码的第二个绑定中加上?问号)。

© www.soinside.com 2019 - 2024. All rights reserved.