【问题标题】:Gatsby GraphQL custom date formatStringGatsby GraphQL 自定义日期格式字符串
【发布时间】:2020-07-06 00:14:41
【问题描述】:

我有这个:

export const pageQuery = graphql`
  query {
    site {
      siteMetadata {
        title
      }
    }
    allMdx(sort: { fields: [frontmatter___date], order: DESC }) {
      edges {
        node {
          excerpt
          fields {
            slug
          }
          frontmatter {
            date(formatString: "DD 'de'  MMMM, YYYY", locale: "pt")
            title
            description
          }
        }
      }
    }
  }
`

在行->> date(formatString: "DD 'de' MMMM, YYYY", locale: "pt") 我必须插入字符串,但这个“de”不起作用。我知道我想显示如下日期: 25 de March, 2020. 但结果是: 25 '32' March, 2020. 我知道这不起作用,我知道为什么,但我无法让它正确。 我正在使用带有 graphql 的 Gatsbyjs

【问题讨论】:

    标签: javascript graphql gatsby date-formatting template-literals


    【解决方案1】:

    盖茨比依赖moment.js to format dates

    要对格式字符串中的字符进行转义,可以将自定义字符串括在方括号中。

    date(formatString: "DD [de]  MMMM, YYYY", locale: "pt")
    

    【讨论】:

    • 非常非常非常非常非常感谢。保持安全兄弟
    【解决方案2】:

    简单的把

    date(formatString: "DD MMMM, YYYY")

    Output Like this: 04 February, 2021

    【讨论】:

    • 在巴西,我们实际上使用的日期输出格式有点像这样:2021 年 2 月 2 日(例如......但无论哪种方式都非常感谢:D)
    猜你喜欢
    • 2019-02-16
    • 1970-01-01
    • 2021-03-21
    • 1970-01-01
    • 2017-02-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多