【问题标题】:Can I style individual elements of a <FormattedDate />?我可以为 <FormattedDate /> 的各个元素设置样式吗?
【发布时间】:2017-03-01 02:22:58
【问题描述】:

我正在使用 FormattedDate 来显示时间,我想知道是否可以设置时间的每个单独部分。 我设法设置了小时和分钟的样式,但是“AM”和“PM”呢?

我的目标是拥有与所附图片相似的东西

【问题讨论】:

  • 不这么认为。无论如何,这种格式对语言环境并不友好。

标签: javascript reactjs react-intl


【解决方案1】:

我做了一些研究,并在 react-intl 的 github 存储库上发布了一个问题作为问题 这是我得到的答案:

"This is not currently possible, but this is the exact use-case for the 
formatToParts() API that we standardized as part of JavaScript! 
Eventually we'll use this API directly within React Intl."

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/DateTimeFormat/formatToParts

替代解决方案:

在研究过程中,我发现了另一个有趣的 NPM 模块,名为 DateFormat 该工具非常灵活,我能够通过设置日期的每个元素的样式来获得所需的结果

【讨论】:

    猜你喜欢
    • 2011-04-16
    • 2012-02-23
    • 1970-01-01
    • 2014-06-02
    • 1970-01-01
    • 1970-01-01
    • 2022-12-03
    • 2019-06-21
    • 2015-06-25
    相关资源
    最近更新 更多